CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    Can a derived class contain a vector of pointers to the parent class?

    Lets say I have a parent class called:

    class Car
    {
    .....
    };

    Then there is a derived class called Toyota, which has a method returning a pointer to the parent class and a vector of pointers of the parent class.

    class Toyota : class Car
    {
    public:

    *Car GetCar(char* model);

    private:
    std::vector<Car*> cars;
    };

    Is this allowed?

    I came across this programming question which requests for a class and a derived class where the derived class has a container of pointers to the parent class and individual elements of the container has to be accessed quickly.
    (This question is neither a school assignment nor a test)

    :s11:
    Last edited by ZhiYi; July 1st, 2010 at 03:02 PM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can a derived class contain a vector of pointers to the parent class?

    Sure it's allowed, but in that specific example I'd say it's a bit fishy.

  3. #3
    Join Date
    Apr 2005
    Posts
    200

    Re: Can a derived class contain a vector of pointers to the parent class?

    You mean the code looks strange? Are there better alternatives to the above code?

    I do feel that the question is a little odd though.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can a derived class contain a vector of pointers to the parent class?

    Well, I'd expect a Toyota object to represent a car which happens to be a Toyota. So I have my Toyota sitting in front of me, and I ask it to go get a different car based on model name? Does that make any sense?

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Can a derived class contain a vector of pointers to the parent class?

    Quote Originally Posted by Lindley View Post
    Well, I'd expect a Toyota object to represent a car which happens to be a Toyota. So I have my Toyota sitting in front of me, and I ask it to go get a different car based on model name? Does that make any sense?
    Sure! It had babies!

    Viggy

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Can a derived class contain a vector of pointers to the parent class?

    I think that the composite pattern is an example of a pattern where you would have that kind of a situation where a particular instance of a derived class would have an array or list of pointers to other objects with a common base class.
    http://en.wikipedia.org/wiki/Composite_pattern

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured