![]() |
|
#1
|
|||
|
|||
How to reach a member function of objects in a vector?Hi all,
I'm trying to create a vector of objects but I didn't figure out how to reach the member function of objects inside the vector. Here is my code: CPP / C++ / C Code:
Now, I would like to reach the member functions of objects in each position of the vector nodes (nodes[0], nodes[1]...). Thanks in advance for the replies. |
|||
|
#2
|
|||
|
|||
Re: How to reach a member function of objects in a vector?You can use operator[]() or at(). Both return an element at the index position you pass as an argument, only at() will throw an exception if the index is out of range.
For example CPP / C++ / C Code:
Code:
|
|
#3
|
|||
|
|||
Re: How to reach a member function of objects in a vector?Quote:
I think the most common is by using the [] index notation. See Footnote. If you have a member function getId() that retrieves whatever it is that gets set by setId(), then just CPP / C++ / C Code:
Note that your use of new inside the loop is unnecessary, and, in fact since you didn't free the memory before the next time through, it represents a memory leak. Why not just declare an object, not a pointer? Regards, Dave Footnote: The [] index notation is convenient, but it does not do bounds checking. If you try to use an index value less than zero or greater than or equal to the size of the vector, the result is undefined behavior. The loop that I showed in the example is absolutely safe since it is guaranteed to use valid index values. In other applications, it might be safer to use vector member function at(), which will throw an exception if its argument causes it to try to access memory outside the array boundaries: Here's the equivalent print statement using at() instead of the index notation. CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: How to reach a member function of objects in a vector?Thank you very much for the very clear responses.
|
|
#5
|
||||
|
||||
Re: How to reach a member function of objects in a vector?Quote:
Here is an implementation of storing objects in a vector and invoking member functions of those objects in a couple of trivial functions; one by accessing the index of the object in the vector and the other by using an iterator. CPP / C++ / C Code:
Output: Code:
MxB |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| Problem executing nam-1.13 | RodolfoAlvizu | Computer Software Forum - Linux | 20 | 28-Feb-2009 16:23 |
| Major newbie problem | cynack | MS Visual C++ / MFC Forum | 1 | 08-Apr-2007 12:25 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 07:17 |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 16:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The