Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ vector of objects

Name: Anonymous 2009-07-13 18:46

How the FUCK do I use the iterator (or anything) to access a method of an object contained in a vector (or even a list)??? I've seen it done but for some reason it still won't work in my code...

Also:  How do I maintain a vector/list of dynamically created (ie. with new) objects... the vector/list would be a member of another class, and I would use methods of that class to add or delete objects from that vector or list.

This part may also be what I'm doing wrong, but the iterator thing keeps giving me shit too.  I will post my code in pastebin if it still won't work.

Name: Anonymous 2009-07-13 19:07

OK /prog/ What the fuck is going on? There are 5 Sepples threads on the front page.

Name: Anonymous 2009-07-13 21:22

>>1
Use poonters

Name: Anonymous 2009-07-13 23:20


#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
using namespace std;
using namespace boost;

class Foo
{
public:
    Foo() {};

    void Bar()
    {
        cout << "Foo::Bar() called!" << endl;
    };
};

int main()
{
    vector< shared_ptr<Foo> > vect;

    vect.push_back(shared_ptr<Foo>(new Foo()));
    vect.push_back(shared_ptr<Foo>(new Foo()));
    vect.push_back(shared_ptr<Foo>(new Foo()));
    vect.push_back(shared_ptr<Foo>(new Foo()));
    vect.push_back(shared_ptr<Foo>(new Foo()));

    for(vector< shared_ptr<Foo> >::iterator it = vect.begin();
        it != vect.end(); ++it)
        (*it)->Bar();

    return 0;
}

Name: Anonymous 2009-07-14 0:07

>>4
#include <boost/shared_ptr.hpp>
using namespace boost;
example program

2/10

Name: Anonymous 2009-07-14 2:01

>>4
No, that doesn't do what I want.  I put it into less shitty words here:

I have 2 classes, and ClassA has a vector of ClassB's as one of its members (and an iterator).

ClassA also has a method to create new ClassB's (ie. using the new keyword) and store them in the vector, as well as a method to remove a ClassB from this vector.  And ClassA also has some function that will need to access a method from a ClassB object that is in the vector.

I've tried a few different ways, but I can't get the iterator to let me access a method of one of the vector objects.  I'm also not sure whether I'm adding the right thing to my vector.I  've read about doing this, checked some pages on Google where people were having similar troubles doing it properly, but still can't get shit to work.

I will post my crappy code if necessary.

Name: Anonymous 2009-07-14 2:45

An iterator has pointer-like semantics

for (std::vector<B>::iterator i = my_vec.begin();
      i != my_vec.end(); ++i) { i->some_method_of_B(...); }

Name: Anonymous 2009-07-14 3:13

1. Dereference iterator, receive object.
2. Use object as usual.

Name: Anonymous 2009-07-14 3:59

DON'T HELP HIM

Name: Anonymous 2009-07-14 8:00

>>7
for_each(vec.begin(), vec.end(), std::mem_fun(&B::some_method));

OMG OPTIMIZED SEPPLESIZED

Name: Anonymous 2009-07-15 0:09

>>7,8
word

still need to know whether I'm doing this shit properly as far as making a new ClassB and storing it in the vector goes.

for (std::vector<B>
ok, so, do I not need to use B* as the type or something?

Name: Anonymous 2009-07-15 1:22

>>6
>ClassA has a vector of ClassB's
>ClassA also has a method to create new ClassB's (ie. using the new keyword) and store them in the vector

So what you really mean is, ClassA has a vector of _pointers_ of ClassB; it has 'ClassB*'s.

Remember, when you dereference an iterator, you get back the class member of the vector; in other words, a ClassB*. So use it like this:

ClassB* obj = *it;
obj->some_method();

or simply:

(*i)->some_method()

>>11
Yes, if you're storing 'new ClassB's, then you're storing ClassB*, so it should be std::vector<ClassB*>.

Just post the damn code. If you're asking such a basic question, your code is worth nothing anyway.

Name: Anonymous 2010-11-26 19:46

Name: Anonymous 2010-12-17 1:35

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Don't change these.
Name: Email:
Entire Thread Thread List