|
-
February 17th, 2004, 06:21 PM
#1
vector and class objects
Hi all,
I am learning the fundamentals of STL my question is:
In a vector container, I can manipulate and ''cout' the contents
of my vectors for built-in data types.
I learned how to move in the vector with iterators.
but how to apply my class function to an object
pointed by the iterator?
DO I need a overloaded operator << ??
Thanks,
Odie
-
February 17th, 2004, 06:30 PM
#2
No, like this...
string zzz;
for (vector<string>::const_iterator cit = versionInfo_.begin);cit != versionInfo_.end(); cit++){
zzz = *cit;
}
Last edited by mburke; February 17th, 2004 at 10:54 PM.
-
February 17th, 2004, 07:01 PM
#3
Thanks for your reply but I could not follow you on this.
where that cntObj.version(...) came from?
I need a little more elaboration on that if possible...
These are my first steps on STL...
-
February 17th, 2004, 10:53 PM
#4
Sorry, I used some of my code. versionInfo_ is the vector variable.
vector<string> versionInfo_;
I simplified the code a bit.
-
February 18th, 2004, 03:16 AM
#5
mburke,
I would like ask mburke a question..
mburke s example I think still does not answer the OP..I think he meant the class function not a variable, or am I misunderstanding the OP ?
Thanks a lot
-
February 18th, 2004, 08:32 AM
#6
I think he wants to know how to assign the object from the vector into an object outside the vector.
string a;
a = *vobj;
-
February 18th, 2004, 09:00 AM
#7
Thanks mburke,
-
February 18th, 2004, 10:38 AM
#8
Sorry for not being clearer on my post.
Yeah, I want to assign my vector to the object the iterator is
pointing to:
- Assuming I have a vector
vector<Box> vBox;
vector<Box>::iterator iBox = vBox.begin();
now I want to apply, say, Box::volume() function to
the first object in my vector vBox;
Box aBox;
aBox = *iBox; // Is this OK? I dont have yet an overloaded
// '=' operator yet.
so...to get my volume
aBox.volume(); // Am I on the right track here?
Counting on you guys for some light...
Thank you,
Odie
-
February 18th, 2004, 11:31 AM
#9
Originally posted by odiesback
Code:
Box aBox;
aBox = *iBox; // Is this OK? I dont have yet an overloaded
// '=' operator yet.
so...to get my volume
aBox.volume(); // Am I on the right track here?
Do you want to change the first item directly, or do you want to get a copy of the item and change the copy?
The line:
Calls the Box class assignment operator and assigns one Box to another Box. Just as if you did this:
Code:
int i;
int j;
//...
j = i;
You are not accessing i, you are assigning to j the value of i. There is no difference in concept. Therefore, if you want to change a copy of the item in the vector, then you are correct with doing this.
However, if you want to change the actual item in the vector, you need to get a pointer to the item in the vector. Just as in the "int" example above, if you wanted to change the value of i using j, then j must be a pointer and you need to get the address of i. Again, no difference in concept when using vector.
To get a pointer to the element in the vector:
Code:
Box *pBox = &vBox[ some_array_index ];
The reason why this works is that vector's store their data in contiguous memory, just like an array. So to get the address of a particular item in the vector, you say &vBox[ 0 ] for the first element, &vBox[ 1 ] for the second element, etc.
Also, you only need to code an assignment operator if the members of your class are not safely copyable. For example, members that point to dynamically allocated memory need to be handled explicitly by a user-defined assignment operator. When you code an assignment operator, you also need to code copy constructor and destructor (rule of three).
Basically, you need to code assignment op, copy ctor and destructor if your Box class fails this simple test (either compilation or when running the app):
Code:
int main()
{
Box a;
// Give a some values...
//...
// Now create another box from a
Box b = a;
// Now assign a box to another
Box c;
c = a;
} // Destroy all three boxes a, b, and c
This tests the integrity of your copy constructor, assignment, and destructor. Either the code will not compile because these operations are not available (i.e. they have private access), or if this does compile, you may get a nasty crash, memory leak, or some other run-time problems doing the assignment, construction, or destruction.
Regards,
Paul McKenzie
-
February 18th, 2004, 11:31 AM
#10
That's right.
aBox = *iBox;
-
February 18th, 2004, 11:50 AM
#11
Thank you for the very detailed explanation and for
keeping me in the game
I was able now to use the iterator to retrieve and
assign my object to another object and then process it.
Also thanks for the tip how to test my class...
Odie
-
February 18th, 2004, 12:30 PM
#12
Also note that you can use the iterator directly, you do
not need to create a new copy or do an assignment :
Code:
vector<Box>::iterator iBox = vBox.begin();
iBox->volume();
-
February 18th, 2004, 12:42 PM
#13
Re: vector and class objects
Originally posted by odiesback
I am learning the fundamentals of STL my question is:
In a vector container, I can manipulate and ''cout' the contents
of my vectors for built-in data types.
You might want to take a look at the following introduction...
-
February 18th, 2004, 01:38 PM
#14
all your help has been greatly appreciated!!!
"->" op... how come didnt I think of it???
Thank you all,
Odie
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
|