Thread: vector with pointers to objects?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    vector with pointers to objects?

    Hi, Im trying to create at vector of objects... the vector should contain objects from B and C.

    Code:
    class A
    {
    // code
    }
    
    class B:A
    {
    // code
    }
    
    class C:A
    {
    //
    }
    so my Q is do I need to create a pointer vector of Obj A?

    Can I create class A like:
    Code:
    class A
    {
      vector <A *> aVec;
    }
    and fill it with objects of B and C from main()? ITC how?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to make use of polymorphism, then yes, though you should prefer a vector of smart pointers, or a Boost ptr_vector instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 03:29 PM
  2. Replies: 25
    Last Post: 04-29-2008, 06:32 AM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. removing objects from std::vector
    By xagiber in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2002, 06:03 PM