1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help in Vectors

Discussion in 'C' started by Pritha, Jul 7, 2011.

  1. Pritha

    Pritha New Member

    Joined:
    Jul 7, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I hava a base class and a derived class as below:
    Code:
    class base
    {
    //member fns
    }
    class derived::public base
    {
    //member fns
    }
    
    Now i have created a vector pointer for base class
    std::vector<base*> pBase;
    And an instance for derived class as well
    derived* pDerived = new derived();

    My query is :
    is it possible to add the derived class object pointer in to the base class vector
    i.e. pBase.push_back(pDerived);
    Kindly explain me on this.
     
    Last edited by a moderator: Jul 7, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes of course, that's the whole point of polymorphism.
     

Share This Page