CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    May 2005
    Posts
    25

    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?

  2. #2
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Help with Vectors of Classes and Dervived Classes

    Quote 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.
    Quote 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
    Quote 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

  3. #3
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Help with Vectors of Classes and Dervived Classes

    Also have a look at Builder design pattern.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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

  5. #5
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    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

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Help with Vectors of Classes and Dervived Classes

    Quote Originally Posted by Ejaz
    Also have a look at Builder design pattern.
    Are there any code samples available for the link?

    Arjay

  8. #8
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Help with Vectors of Classes and Dervived Classes

    Quote Originally Posted by Arjay
    Are there any code samples available for the link?

    Arjay
    There it is.

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Help with Vectors of Classes and Dervived Classes

    Quote 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.

    Quote 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.

  10. #10
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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():

    Code:
    intvector.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

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Help with Vectors of Classes and Dervived Classes

    Quote 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

  12. #12
    Join Date
    May 2005
    Posts
    25

    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.

  13. #13
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    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.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  14. #14
    Join Date
    Feb 2003
    Posts
    377

    Re: Help with Vectors of Classes and Dervived Classes

    Quote 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.

    Quote 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).

    Quote 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.

  15. #15
    Join Date
    May 2005
    Posts
    25

    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.

Page 1 of 3 123 LastLast

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