implement clone vector in Java using VC++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gooseman
    New Member
    • Sep 2007
    • 1

    implement clone vector in Java using VC++

    Hi there!

    Are there any equivalent functions in C++ that is similar to clone() in Java?

    In VC++,

    I have 2 vectors with different types namely,

    std::vector<Cla ssA *> v1;
    std::vector<Cla ssB *> v2;

    Is there anyway that i can assign v2 to v1 given that the vectors are of different types?

    I know it is easy in Java, since at the initial declaration of the vectors, you don't
    have to supply what type of objects are to be inserted.

    Ex.
    Vector v1 = new Vector();
    Vector v2 = new Vector();

    ClassA a = new ClassA();

    v1.add(a);

    ClassB b = new ClassB();

    v2.add(b);

    v2 = (Vector) v1.clone();

    Any suggestions?

    Thanks very much.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You cannot a container of one type to a container of another type.

    You can't do it in Java either. All Java does is create a new container for the new type using the name of the original container and placeas the original container in the garbage bin. That isn't an assignment.

    An assignment requires that the target and the source have the same contents after the assignment operation. That is, that the target is a copy of the source. The target cannot be a copy of the source if the type changes.

    You can have two containers of different types or the same types in C++ but the containers cannot have the same names.

    Comment

    Working...