Still a little bad with pointers but i think i need them here.

okay so my code looks something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class a{

std::vector<entity> b();

public:
void getentities(int i);

};

void a::getentities(int i){
    return entity[i];
}


and then in my main i need to update the entity object at some points but as is, im passing a copy rather than the entity itself. I guess i need pointers but i've found them a hard concept to grasp.

Any help would be great cheers,

chris
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class a{

std::vector<entity> b();

public:
int num;
void getentities(int i);

}obj;

//must have matching return type
entity* a::getentities(int i){
    return &entity[i]; //returns the address
}

int main(){
    entity *b = obj.getenties(0); //entity pointer b now points to the 1st object in the vector
    //use '->' because b is pointing to an object
    b->num = 5;
    return 0;}


Some helpful pages:
http://cplusplus.com/doc/tutorial/pointers/
http://cplusplus.com/doc/tutorial/polymorphism/
Hi chr15chr15,

from what I can tell from your code, I think you want your member function getentities to return the entity stored in your implementation object b, which upon instantiation of the class is an empty vector<entity> called b.

If so, I think you want something more like the following:

1
2
3
4
5
6
7
8
9
10
11
12
class a
{
public:
      a(const std::vector<entity>& v = std::vector<entity>())
           : b(v) {}

      entity& operator[](size_t i) {return b[i];}
      const entity& operator[](size_t i) const {return b[i];}

private:
      std::vector<entity> b;
};


I included a constructor to overload the synthesised version, which will simply initilise b to a default value, whilst here you initialise the vector yourself via the member ininitialisation list.

You can access the elements of b via the index operators (e.g,, a[2]), implicitly inlined here, and alter those values (for non-const objects of class a) by assigning the returned reference of the non-const operator. Of course if the object is constant then the const index operator will be used and you wont be able to assign the returned reference (which is a good thing!). You can then use the index operators in main.cpp to alter the entries in a::b as I have described above.

If you expect a::b to be very large you may want to use a pointer to vector<entity> as your a::b to keep your objects manageable. Then all operations will have to be redirected through that pointer. For example,

1
2
3
4
5
6
7
8
9
10
11
12
class a
{
public:
      a(const std::vector<entity>& v = std::vector<entity>())
           : b(new std::vector<entity>(v)) {} //assigns b to a pointer, pointing to a copy of v

      entity& operator[](size_t i) {return (*b)[i];} //must dereference b before accessing vector it points to
      const entity& operator[](size_t i) const {return (*b)[i];} //must return const entity& here otherwise users can still alter returned value!

private:
      std::vector<entity>* b; //pointer to vector resource
};


Hope this all helps. Good luck!
Topic archived. No new replies allowed.