CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jan 2004
    Location
    Murrieta, CA
    Posts
    16

    Question 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

  2. #2
    Join Date
    Jun 2003
    Posts
    188
    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Murrieta, CA
    Posts
    16

    Smile

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

  4. #4
    Join Date
    Jun 2003
    Posts
    188
    Sorry, I used some of my code. versionInfo_ is the vector variable.

    vector<string> versionInfo_;

    I simplified the code a bit.

  5. #5
    Join Date
    Feb 2004
    Posts
    138
    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

  6. #6
    Join Date
    Jun 2003
    Posts
    188
    I think he wants to know how to assign the object from the vector into an object outside the vector.

    string a;

    a = *vobj;

  7. #7
    Join Date
    Feb 2004
    Posts
    138
    Thanks mburke,

  8. #8
    Join Date
    Jan 2004
    Location
    Murrieta, CA
    Posts
    16
    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

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    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:
    Code:
    aBox = *iBox;
    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

  10. #10
    Join Date
    Jun 2003
    Posts
    188
    That's right.

    aBox = *iBox;

  11. #11
    Join Date
    Jan 2004
    Location
    Murrieta, CA
    Posts
    16

    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

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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();

  13. #13
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

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

  14. #14
    Join Date
    Jan 2004
    Location
    Murrieta, CA
    Posts
    16
    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
  •  





Click Here to Expand Forum to Full Width

Featured