|
-
May 1st, 2005, 05:48 PM
#1
Help with Vectors of Classes and Dervived Classes
Parent Class is Component
Child Classes are Pump, Water, System, Column, Environment
Since all my objects are Components, can I make an vector of Components and use it for any type of Object which I have?
i.e.
vector<CComponent> ComponentList
CPump NewPump
ComponentList.push_back(NewPump)
Is this something doable?
Also
If I wanted to dynamically allocate Components while the program is running, do I have to allocate the Component dynamically, or is that handled within the vector when I push_back?
-
May 1st, 2005, 06:01 PM
#2
Re: Help with Vectors of Classes and Dervived Classes
 Originally Posted by d4m
Since all my objects are Components, can I make an vector of Components and use it for any type of Object which I have?
Nope. In C++ that won't work. It will compile and run, but what is actually stored in your vector will be copies of the CComponent part of your other classes. The objects will be sliced.
 Originally Posted by d4m
vector<CComponent> ComponentList
CPump NewPump
ComponentList.push_back(NewPump)
Is this something doable?
Yes it is, but not that way for the reason explained above. You need to store pointers to dynamically allocated objects instead.
Code:
vector<CComponent*> ComponentList
ComponentList.push_back(new CPump);
But now remember to delete the object before you remove it from the vector, or let the vector go out of scope.
Code:
delete ComponentList.back(); // Delete the last object
ComponentList.pop_back(); // remove the pointer to it from the vector
 Originally Posted by d4m
If I wanted to dynamically allocate Components while the program is running, do I have to allocate the Component dynamically, or is that handled within the vector when I push_back?
All memory allocation of the object type you store will be handled by the vector. It will not though handle memory pointed to by pointers you store in it. (hence the delete above is needed)
Insert entertaining phrase here
-
May 1st, 2005, 11:18 PM
#3
Re: Help with Vectors of Classes and Dervived Classes
Also have a look at Builder design pattern.
-
May 4th, 2005, 07:53 AM
#4
Re: Help with Vectors of Classes and Dervived Classes
An important point to notice while popping members from the vector -
Wein suggested :
<CODE>
delete ComponentList.back(); // Delete the last object
ComponentList.pop_back(); // remove the pointer to it from the vector
</CODE>
When u have such a vector, its quite tempting to pop the elements and then call the delete on them. This can be disastrous.
The reason - because the objects inside a vector are copies of the original object that you had pushed into the vector. The members that you force to pop out of the vector are copies too !
This creates a memory leak situation. Bang !
Regards,
Exterminator
-
May 4th, 2005, 08:41 AM
#5
Re: Help with Vectors of Classes and Dervived Classes
In this case that wouldn't matter. The objects here are pointers, so wheter you delete the original pointer or a copy of it doesn't matter. They both point to the same address in memory.
Or did I misinterpret your post?
Insert entertaining phrase here
-
May 4th, 2005, 06:16 PM
#6
Re: Help with Vectors of Classes and Dervived Classes
Code:
The members that you force to pop out of the vector are copies too !
I'm a little confused... pop_back() doesn't return a copy of the popped object, it simply removes it from the vector...
Old Unix programmers never die, they just mv to /dev/null
-
May 4th, 2005, 07:34 PM
#7
Re: Help with Vectors of Classes and Dervived Classes
 Originally Posted by Ejaz
Also have a look at Builder design pattern.
Are there any code samples available for the link?
Arjay
-
May 4th, 2005, 11:16 PM
#8
Re: Help with Vectors of Classes and Dervived Classes
 Originally Posted by Arjay
Are there any code samples available for the link?
Arjay
There it is.
-
May 5th, 2005, 04:17 AM
#9
Re: Help with Vectors of Classes and Dervived Classes
 Originally Posted by wien
In this case that wouldn't matter. The objects here are pointers, so wheter you delete the original pointer or a copy of it doesn't matter. They both point to the same address in memory.
Right. In this case that wont be the matter of worry since we are storing the pointers to the objects. I guess I was referring to objects being stored in the vector scenario in my post.
 Originally Posted by HighCommander4
I'm a little confused... pop_back() doesn't return a copy of the popped object, it simply removes it from the vector...
Yes, HighCommander4. I was not clear. What i said is what u interpreted and that was wrong. So I was wrong.
What I really meant was something like this:
Code:
#include<iostream.h>
#include<string.h>
#include<vector>
using std::vector;
using std::string;
class myInt{
protected:
int i;
public:
myInt(int i):i(i){
}
int getMyInt(){
return i;
}
};
int main(){
vector<myInt> intvector;
myInt *intA = new myInt(10);
for (int loopindex = 0; loopindex<10;loopindex++){
//I create myInt object by dynamically allocating memory and push them into the vector
myInt *newmyint = new myInt(loopindex);
intvector.push_back(*newmyint); //In this case what goes in the vector is a copy of the original newmyint object
//need to call a delete on newmyint here - else memory leak;
delete newmyint;
}
for (int j=0;j<10;j++){
cout << intvector[j].getMyInt()<<endl;
}
//Just use one of the two for loops below at a time... to get the difference in the approaches
//One way to clear the vector
//This is what was suggested by Wein.
for(int loopindex1 = 0;loopindex1<10;loopindex1++){
//do whatever processing required on the last member or whatever
try{
//here normal pop would do...
intvector.pop_back();
}catch(char* str){
cout << str;
}
}
//Other way of clearing the vector
//I dont know why I would do this but just in case.
for(int loopindex2 = 0; intvector.size()>0 && loopindex2<10;loopindex2++){
*intA = intvector[intvector.size()-1]; //In this case what we get is a copy of whats there in the vector;
//do some rigourous processing on this intA object and then i wish to free the memory...
delete intA;
intvector.pop_back();
}
return 0;
}
I guess the comments inside the code explain the steps.
I accept I was wrong earlier in my words ... it sounded different in my head from what i struck on the keyboard.
Well, again, if anyone thinks I need to correct myself here ... DO REPLY BACK ...
Thanks a lot.
Exterminator.
-
May 5th, 2005, 05:17 PM
#10
Re: Help with Vectors of Classes and Dervived Classes
I'm sorry but I don't understand your second way of clearing the vector? Why are you calling delete on each element when you actually called delete on the original objects when you populated the vector? The vector performs its own memory management, and when it stores objects (like in your example), it allocates and deallocates memory for those objects automatically. So what you really are doing when you populate the vector is create a new-ed object, push a copy of it onto the vector then delete the original. Managing the copy is done by the vector itself, and you don't need to worry about it: you deleted the original object. So in your last loop you are actually causing delete to be called twice on the object: once by you and once by the vector, which of course is undefined behaviour.
The simplest way to clear the vector in your case is to call clear():
This is equivalent to (and most likely more efficient than) calling pop_back() for each element in a loop. You don't need to worry about deleting anything because the vector stores objects, not pointers.
Old Unix programmers never die, they just mv to /dev/null
-
May 9th, 2005, 02:11 AM
#11
Re: Help with Vectors of Classes and Dervived Classes
 Originally Posted by HighCommander4
Why are you calling delete on each element when you actually called delete on the original objects when you populated the vector?
I just wished to show some wrong usage to explain that vectors do keep copies of objects being pushed into them. Just wanted to show how we can make mistakes .. i guess i might not have been able to give a very sound example in this case but i have seen people do this.. they get object out the vector into some temp object and then kill the temp one and remove the member from the vector once they are done (no clearing of the whole vector coz they might use the rest of the members for some other processing or something) and this leave them with the memory mess-ups.
Anyways .. i guess the topics closed now !
Cheers,
Exterminator
-
May 9th, 2005, 08:17 AM
#12
Re: Help with Vectors of Classes and Dervived Classes
Alright.
Cleaning the entire vector I got.
Lets say there is 20 elements in the vector. All of it Dynamically allocated but at different times during the run.
I want to delete the element with index 5 from the vector.
Code:
vector<MyClass*> MypClassVector
delete MypClassVector[5]; <--- simple as that?
Or do I need to physically pop off each member before I can free the memory?
Another question, since I am allocating the pobjects themselves as new and not the members of the class as new, do I need to worry about making a copy constructor?
When passing the vector to a function, how should I pass it? Does it operate like typical vars as I can pass it by value, reference or pointer?
Last edited by d4m; May 9th, 2005 at 08:26 AM.
-
May 9th, 2005, 09:01 AM
#13
Re: Help with Vectors of Classes and Dervived Classes
If you want to create a vector of abstract based pointers, I recommend you use a smart pointer like that in the following link:
http://code.axter.com/arr_ptr.h
The above smart pointer is specifically designed to work with an abstract pointer being used in a STL container like std::vector.
The header has example code, and a link to an example program.
By using a smart pointer like arr_ptr, you don't have to worry about deleteing the pointer, because the clean will be done automatically.
-
May 9th, 2005, 11:25 AM
#14
Re: Help with Vectors of Classes and Dervived Classes
 Originally Posted by d4m
Or do I need to physically pop off each member before I can free the memory?
That will work, but then you will have a pointer to deleted memory in your vector. It is probably better to remove the deleted pointer from the vector to ensure that you don't try to use it again later.
 Originally Posted by d4m
Another question, since I am allocating the pobjects themselves as new and not the members of the class as new, do I need to worry about making a copy constructor?
Regardless of how you allocate space for your objects, you should always look at your classes to determine whether they require a custom copy constructor, assignment operator, or destructor. It depends on you class and how you intend for it to behave. For example, if it has dynamically allocated memory that you want copied when the object is copied, then you must provide your own copy constructor, copy assignment operator, and destructor (or stop using dynamically allocated memory in favor of copyable smart pointers or containers).
 Originally Posted by d4m
When passing the vector to a function, how should I pass it? Does it operate like typical vars as I can pass it by value, reference or pointer?
It is like a regular object. Generally, passing by const reference is appropriate so that you don't copy the entire vector as you would passing by value. If you need to modify the vector in the function, you can pass by non-const reference instead.
-
May 9th, 2005, 12:08 PM
#15
Re: Help with Vectors of Classes and Dervived Classes
Thank you Jlou.
Axter: Since I'm still learning the concept of pointers, I really wanted to do the allocating and deallocating by hand in this version. Figured it would be a good exercise.
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
|