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.
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.
Re: deriving from vectors
Quote:
Originally Posted by
nuzzle
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.
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.
Re: deriving from vectors
Quote:
Originally Posted by
thefollower
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.
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?
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>.
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
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.
Re: deriving from vectors