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!