|
-
July 16th, 2012, 02:57 AM
#1
Subclass objects in one array?
Hi
i want to save similar objects, meaning having same superclass, in an array. Example: I have a superclass vehicle. Class car and truck inherit from vehicle. Now i have a few from both of them an want to put them in an array, vector, set or else. Is this possible in C++ or are there an workarounds? When i remember right, Java and C# offer this feature.
Here again an example:
Code:
class vehicle{
int mMaxSpeed;
}
class truck : public vehicle{
int mTrailerLength;
}
class car : public vehicle{
bool mHasHitch;
}
main()
{
truck mytruck = new truck();
car mycar = new car();
vector<vehicle> myCarPool;
myCarPool.pushback(mytruck);
myCarPool.pushback(mycar);
}
Greets
-
July 16th, 2012, 03:22 AM
#2
Re: Subclass objects in one array?
You can't store objects of different classes into a vector but you can store a base pointer. To avoid having memory leaks due to forgotten deletes you can make the vector store a smart pointer like boost::shared_ptr http://www.boost.org/doc/libs/1_50_0...shared_ptr.htm or std::shared_ptr if your compiler supports them. http://en.cppreference.com/w/cpp/memory/shared_ptr
Last edited by S_M_A; July 16th, 2012 at 03:25 AM.
-
July 16th, 2012, 03:37 AM
#3
Re: Subclass objects in one array?
... Besides, your "syntax" for new and main() looks like to be incorrect. Did you probably mean:
Code:
int main()
{
truck* mytruck = new truck();
car* mycar = new car();
vector<vehicle*> myCarPool;
myCarPool.pushback(mytruck);
myCarPool.pushback(mycar);
...
delete mycar ;
delete mytruck;
return 0;
}
Victor Nijegorodov
-
July 16th, 2012, 07:29 AM
#4
Re: Subclass objects in one array?
 Originally Posted by ImNotaBot
Here again an example:
Note that the vehicle class must have a virtual destructor if you want to store (and ultimately delete, I presume) derived instances using a pointer to the base class.
You can also have a look at the Boost Pointer Container Library.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 16th, 2012, 11:08 AM
#5
Re: Subclass objects in one array?
When i remember right, Java and C# offer this feature.
The method that Java and C# use is similar, under the hood, to what you do in C++.
There is not actually much difference between the concepts of Java & C++.
Both have containers that store one type of object.
In Java, you can only store references to objects in a container.
In C++ you store copies of objects in a container. The difference being that is that the objects stored could be pointers.
Code:
vector<car> myCars; // A vector of 'car' objects. The 'car's are stored in the vector. No equivalent in java.
vector<car *> myPointerCars; // A vector of pointers to 'car' objects. The 'car's are stored elsewhere.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
August 16th, 2012, 05:04 AM
#6
Re: Subclass objects in one array?
Well sorry that I did not answer. Quite impolite... but I am terribly under pressure of time due to exams.
I will come back to the topic as soon as i made them.
I really appreciate your help so thanks to all of you.
@ VictorN: Yes your right :-D i just wrote it down for a quick sloppy illustration of the problem.
I'm hanging around in english forums, becuase i want to _improve_ my english. So please feel free to correct me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|