Accessing Member Methods inside a Vector

This is a discussion on Accessing Member Methods inside a Vector within the C++ Programming forums, part of the General Programming Boards category; Hi guys and gals. It's been a while since I've posted here but I'm back. I'm having some trouble with ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    Thumbs up Accessing Member Methods inside a Vector [RESOLVED]

    Hi guys and gals. It's been a while since I've posted here but I'm back.

    I'm having some trouble with vectors, vector iterators, and vector element access. I know I've done this before but my memory is failing me apparently.

    Okay here's the run down. I have a class that defines how to keep track of an item in an inventory i.e. a box. The class knows the dimensions of the box, the contents of the box, and how the box should be handled. As boxes are received, the newly created instance is pushed onto the vector.

    Now I want to be able to access the elements of the vector through an iterator, which I know how to do. However I need to access certain methods of the box class when I use the iterator, not the entire instance itself. Can someone give me an example of being able to access class methods from a vector iterator or point me in the direction of something that might be able to help? Thanks in advance!
    Last edited by xmltorrent; 06-23-2008 at 12:03 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If it is an iterator over the vector, then *it is the box object, so (*it).whatever().

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    Now that works.

    So access for members created off the stack looks like this:

    (*vectorIteratorNameHere).classMethodNameHere()

    What about classes that are instantiated off the heap? You would essentially push them onto the vector in a similar fashion but would the iterator access be different?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    If your vector contains items from the heap, you should be using a shared pointer implementation as the container type, but other than that, it should be just like accessing members through a pointer to pointer:

    (*it)->foo(); works, as should (**it).foo();

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Or you could use a boost:tr_vector, which manages the memory for you and provides a more pleasant interface.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    Or you could use a boost:tr_vector, which manages the memory for you and provides a more pleasant interface.
    haha I like dirty code! That and I'm not too familiar with the Boost library yet. :/

    But thanks for all your help guys. I'm on the right track now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing a vector inside a vector
    By csonx_p in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 05:15 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  4. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  5. Accessing CEdit Member Functions Inside CEditView ::MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-10-2002, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21