Thread: vector of derived classes

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    vector of derived classes

    hi, i am wondering how to do this or a similar implementation

    eg.

    Code:
    class shape{}
    class square : public shape{}
    class triangle : public shape{}
    
    std::vector<shape> vector;
    
    vector.push_back(square);
    vector.push_back(triangle);
    
    vector[0].render(); //draws a square
    vector[1].render(); //draws a triangle
    i have had little success,
    anyhelp would be apretiated,

    p3p

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Your classes don't define a render() member function.

    2) Class definitions end with a semicolon.

    3) shape, triangle, and square are types. When you declare a vector, you must state the type of the things it will contain, which you did correctly, but you don't add a type to a vector--you add objects to the vector. What you did is similar to this:
    Code:
    int myArray[3];
    myArray[0] = int; //error, should be: myArray[0] = 10;
    4) In order to get polymorphism to work, ie. to be able to call the function in the object's class rather than the function that resides in the class of the variable type the object is assigned to, you need to provide a render() function for each class. Furthermore, to get polymorphic behaviour, which just means you want to call different functions, you have to declare the function in the base class to be virtual.

    When you declare a function as virtual, you are telling the compiler to use "dynamic binding" rather than "static binding". Static binding occurs when the type of the variable determines the class where the function is called. Dynamic binding tells the compiler to call the function in the class of the object that is assigned to the variable.

    So, whenever you want polymorphic behaviour, which simply means you want different functions to be called depending on the type of the object, you should think of three things:

    1) A suitable base class variable to hold the various objects.

    2) A function with the same name but different versions in the various classes.

    3) Declare the base class version of the function virtual.
    (It's also recommended that you declare the versions in the derived classes as virtual, too, so that if you decide to derive classes from them later, you can get polymorphic behaviour with your new derived classes without having to go back and add virtual to the function declarations.)
    Last edited by 7stud; 10-23-2005 at 11:10 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Assuming your code is pseudo-code and you know what 7stud said, you need to hold base class pointers in your vector (e.g. vector<shape*>). Since the vector holds objects by value, if you add a square to a vector<shape>, the vector will hold a copy and the square-ness will be sliced off and you lose the extra data held by the square. Dynamic polymorphism is accessed through references or pointers, and since vectors can't hold references, pointers are the way to go.

    Remember that you must manage the memory if you hold pointers by using a shared_ptr implementation if you can, or by calling new and delete yourself when adding and removing shapes.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The critical point Daved brought up means that point 1) should be corrected to read:

    1) A suitable base class pointer variable to hold the various objects.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    im sorry 7stud i should of stated that it was pseudo code,

    thanks for all you help,

    p3p

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. Deleting derived classes
    By *DEAD* in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2008, 12:37 PM
  3. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM