GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Oct-2009, 09:43
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 73
cpit is on a distinguished road

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:
vector<Node> nodes;
 for (int i=0; i<10; i++){  
 	 Node *ntemp = new Node;   
 	 	ntemp->setId(i); 	 	
     nodes.push_back(*ntemp);
 }

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  
Old 29-Oct-2009, 10:03
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

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:
#include <vector>
#include <iostream>

class Useless
{
public:
    
    void print() { std::cout << "Nah, it's useless. . ."; }
};

int main()
{
    std::vector<Useless> aVector(2);
    aVector.push_back(Useless());
    aVector.push_back(Useless());
    
    aVector[0].print();
    aVector.at(1).print();
    
    aVector.at(23423).print();
    
    return 0;
}
Output on my Windows XP machine:
Code:
Nah, it's useless. . .Nah, it's useless. . . This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
  #3  
Old 29-Oct-2009, 10:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to reach a member function of objects in a vector?


Quote:
Originally Posted by cpit
...Now, I would like to reach the member functions...

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:
int main()
{
    vector < Node > nodes;

    for (int i = 0; i < 10; i++) {
        Node ntemp;
        ntemp.setId(i);
        nodes.push_back(ntemp);
    }
    for (int i = 0; i < nodes.size(); i++) {
        cout << "nodes[" << i << "].getId() = " << nodes[i].getId() << endl;
    }

    return 0;
}

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:
        cout << "nodes.at(" << i << ").getId() = " << nodes.at(i).getId() << endl;
  #4  
Old 29-Oct-2009, 11:25
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 73
cpit is on a distinguished road

Re: How to reach a member function of objects in a vector?


Thank you very much for the very clear responses.
  #5  
Old 29-Oct-2009, 18:07
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: How to reach a member function of objects in a vector?


Quote:
Originally Posted by cpit
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:
vector<Node> nodes;
 for (int i=0; i<10; i++){  
 	 Node *ntemp = new Node;   
 	 	ntemp->setId(i); 	 	
     nodes.push_back(*ntemp);
 }

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.

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:
#include <iostream>
#include <vector>

class Box
{
public:
    Box() : m_h(0), m_w(0), m_d(0) {}
    Box(const size_t height, const size_t width, const size_t depth) :
    m_h(height),
    m_w(width),
    m_d(depth) {}
    friend std::ostream& operator<<(std::ostream& os, const Box& rhs)
    {
        os << rhs.m_h << " " << rhs.m_w << " " << rhs.m_d;
        return os;
    }
    friend std::istream& operator>>(std::istream& is, Box& rhs)
    {
        is >> rhs.m_h >> rhs.m_w >> rhs.m_d;
        return is;
    }
private:
    size_t m_h;
    size_t m_w;
    size_t m_d;

};

typedef std::vector<Box> vBoxes;

void print_vBoxes_idx(vBoxes& v)
{

    for(size_t idx = 0; idx < v.size(); idx++)
    {
        std::cout << v[idx] << std::endl;
    }
}

void print_vBoxes_it(vBoxes& v)
{
    vBoxes::iterator it;
    for(it = v.begin(); it != v.end(); it++)
    {
        std::cout << *it << std::endl;
    }
}

int main()
{
    vBoxes v;
    Box b1(1, 2, 3);
    Box b2(10, 20, 30);
    Box b3(100, 200, 300);
    Box b4;

    v.push_back(b1);
    v.push_back(b2);
    v.push_back(b3);
    v.push_back(b4);


    print_vBoxes_idx(v);
    std::cout << std::endl;
    print_vBoxes_it(v);

    return 0;
}


Output:

Code:
$ ./vobjects 1 2 3 10 20 30 100 200 300 0 0 0 1 2 3 10 20 30 100 200 300 0 0 0


MxB
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:30.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.