CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2012
    Posts
    42

    deriving from vectors

    i want to derive a class named population which is derived from vectors of solution and solution class is derived from vectors of int. that means that population is collection of solutions and is created with vector by deriving it from template based vectors<solution>. my question is that im bit confused in this hierarchy and not getting idea of constructors and destructors and calling mechanism of methods. i do have concepts of inhertince and constructors in inheritence but confused in this perspective.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: deriving from vectors

    It sounds like what you should do is to define a solution class that has a vector of int as a member variable, then define a population class that has a vector of solution objects as a member variable.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: deriving from vectors

    You shouldn't derive from STL containers such as vector. They lack virtual destructors which opens up for memory leaks and undefined behaviours.

    Instead base your design on classes you define yourself and use STL containers for implementation purposes only.

  4. #4
    Join Date
    Apr 2012
    Posts
    29

    Re: deriving from vectors

    Quote Originally Posted by nuzzle View Post
    You shouldn't derive from STL containers such as vector. They lack virtual destructors which opens up for memory leaks and undefined behaviours.
    Could that be followed by an example please ?

    I can't imagine how a coder freed the allocated objects used in the derived class has no influence over their base objects' remains.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: deriving from vectors

    Quote Originally Posted by thefollower
    Could that be followed by an example please ?
    Attempting to delete a derived class object through a pointer to its base class results in undefined behaviour if the base class destructor is not virtual. Since it is undefined behaviour, an example does not guarantee that you'll see any specific results.

    Quote Originally Posted by thefollower
    I can't imagine how a coder freed the allocated objects used in the derived class has no influence over their base objects' remains.
    Suppose the derived class destructor frees some memory. Now, you use delete on a pointer to the base class that points to this derived class object. We could speculate that perhaps only the base class destructor is called; after all, it is non-virtual. If this happens, then the memory that should have been freed will not be freed.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: deriving from vectors

    Quote Originally Posted by thefollower View Post
    Could that be followed by an example please ?
    It's stated in the C++ standard (5.3.5 delete, paragraph 3):

    "... if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined..."

    This means that if you do,

    Code:
    class A {};
    class B : public A {};
    //
    A* x = new B;
    delete x;
    you have undefined behaviour because A lacks a virtual destructor.

    And since vector doesn't have a virtual destructor you get undefined behaviour if you use it in place of A.

    Note that C++ is significantly different from more OO specialized languages such as Java. C++ doesn't have a common "mother" base class like Java has. It's substantially more important in C++ to make sure a class is designed to act base class before one uses it as such.

    So don't use STL containers polymorphically. Use them as implementation detail only.
    Last edited by nuzzle; July 13th, 2012 at 06:57 PM.

  7. #7
    Join Date
    Jul 2012
    Posts
    42

    Re: deriving from vectors

    i have seen some sample code like class CVector : public std::vector<T> on same lines if we write class solution: public vector <int> now my next question is whether vector<int> is member variable here or some class ? because it looks like it should be member variable but if so then y it is written in class like manner of inheritence?

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: deriving from vectors

    Quote Originally Posted by tspga
    i have seen some sample code like class CVector : public std::vector<T> on same lines if we write class solution: public vector <int> now my next question is whether vector<int> is member variable here or some class ? because it looks like it should be member variable but if so then y it is written in class like manner of inheritence?
    It would be a class, not a member variable; you would be looking at a case of public inheritance from std::vector<int>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2012
    Posts
    42

    Re: deriving from vectors

    so u suggests it being as member variable? but i want to do it in the way i described means like class solution: public vector<int> whats the difference i mean do not tell that ur one is called composition and mine is inheritence i want to ask how to initialize them in my case that is inheritence

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: deriving from vectors

    Quote Originally Posted by tspga
    so u suggests it being as member variable?
    Yes.

    Quote Originally Posted by tspga
    but i want to do it in the way i described means like class solution: public vector<int> whats the difference
    Why do you want to use public inheritance here? nuzzle and I already explained to you why not in posts #3, #5 and #6.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Jul 2012
    Posts
    42

    Re: deriving from vectors

    regards...

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