#members of other classes and pointers

1 messages · Page 1 of 1 (latest)

covert cobalt
#

This is my entity manager class, it has a vector, and a pointer to that vector

class EntityManager {
private:
public:
    std::vector<MapEntity> vAllMapEntities;
    std::vector<MapEntity>* pvAllMapEntities = &vAllMapEntities;
    EntityManager();
    int CountObjectsInMapEntityList();
};```
This is my Map Entity class.
```cpp
MapEntity::MapEntity() :
    mposx(0.0), mposy(0.0), entitybox{200, 100, 50, -50} , render_pointer(&all_trees), pEntManagerMeidList(EntityManager::vAllMapEntities){

    SendToRender();
}```
My aim:
to get each newly created mapentity object to call in its constructor a function to add a variable of itself into the vector of entity manager.

The one thing I am stuck at is in the map entity:

In the constructor of map entity i cant get the syntax right to reference the entity manager vAllMap Entities vector (or the pointer to it), the error message is:

`A non static member reference must be relative to a specific object`

I made sure that the entity manager is called before any mapobject is created.

Is it even possible to do what I want to do?  To repeat the question: I want to get hold of the vector in another class from the constructor of this class or or the pointer (of that other class) pointing to a member of itself (or alternatively have a pointer from this class to the other classes vector. However then I wonder why do coding books say "public members are accessible everywhere".
noble mirageBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

undone beacon
#

This is honestly a bit of a code smell. You probably don't want to know about the manager from the entity

#

For your immediate issue, EntityManager::vAllMapEntities can't be done, since you're trying to use it like it's a static member, but it's non-static

covert cobalt
undone beacon
#

Code Smell = Fishy things in code that likely mean bad design

covert cobalt
undone beacon
#

Yeah, it's using a static member, not something object specific

covert cobalt
#

What puzzles me the most is how coding books talk about how public members of a class are accessible everyhere, and it never works as intented.

undone beacon
#

And again, you probably don't want your objects to know about the manager. Why should they care

undone beacon
covert cobalt
covert cobalt
#

adding itself would mean copy itself into it?

undone beacon
#

yes

#

but that's still not the issue

#

Manager classes tend to be a code smell

#

What is the actual problem you're trying to solve

#

Because if it is what I think it is, this is actually pretty tricky

covert cobalt
#

at a top level, I am trying to create a system where every new map entity is registerted in a vector so I can count the vector, look into each object and so forth. I successfully did something similar in the code: I added a vector before main (I think that would be a global variable), then that vector would be passed through entity functions and add parts of the object (not the whole thing) to it, then that vector would be sent to the render code and render all things.

So I assumed the same approach would work now with entire objects. However I had to rely on declaring the vector an extern so I could access it.

#

I think the main problem is that I dont see a way to have each object when it is created to add parts of itself to a vector if it is done by an outside class.

undone beacon
#

Why not use some existing framework like EnTT?

covert cobalt
undone beacon
#

The big issue with the vector approach is stability of references/iterators or making copies into the vector when you don't intend to

covert cobalt
#

my current next attempt would be to do the same as with my render vector

#

, just use a differetn variable to track objects

#

the reason for needing the vector is to keep track of the position of things because I want to add a system next where there is a camera that can move and objects are rendered based on map position, not screen position, and for that I need to grab all things and their variables

undone beacon
#

in general, you don't want to make copies of an object in it's constructor

#

it's an incomplete object

covert cobalt
#

could you specific what you mean by make copies in a constructor?

#

is it the push_back into the vector?

undone beacon
#

yeah, push_back

covert cobalt
#

would a pointer of the object pointing at itself work that adds itself to the vector? Like is that technically possible?

undone beacon
#

no

#

you'd have to handle copies, moves, destruction etc

#

you basically want the object managing entity lifetime to handle creation of the entity and tracking of it

#
stable_handle allocate() {
  Entity e{};
  tracked_entities.push_back(e);
  return some_handle_for(e); 
}
#

there's several approaches for how to create that stable handle, but it's important for when the track_entities vector resizes and invalidates the existing references/iterators

#

easiest approach would be dynamic allocation, but that's slow (the object pointed to won't move when vector's iterators are invalidated)

#

you can also take the approach of keeping some extra bookkeeping for the vector, and that'll handle moves, deletes, etc. removes the extra dynamic allocation, but it'll cost more memory

covert cobalt
#

if the vector resizes, pointers may point to the wrong thing?

undone beacon
#

iterators of the vector are unstable

#

basically, any modification to the vector (not the contents of it) may result in invalidation

#

So insertion and deletion

#

if you store dynamically allocated objects in it, the vector will hold pointers and you can just pass out the pointers, rather than iterators to an element in the vector

tired bridge
#

(I only read the initial post)
but new perspective:

A simple way to list and inspect your entities is to have
static std::vector<MapEntity*> AllEntities
in the MapEntity class
the MapEntity constructor adds this to the vector, the destructor removes it

tired bridge
#

...
a general tip would be to take a step back and do some reading on the basics of objects and pointers (owning vs non-owning) and containers
it is easy to get confused when you think you already know the basics, but you don't know them 100%

covert cobalt
# tired bridge ... a general tip would be to take a step back and do some reading on the basics...

I have added the

static std::vector<MapEntity*> AllMapEntities;```
to the mapentity class as a public variable, and then
```cpp

MapEntity::MapEntity() :
    mposx(0.0), mposy(0.0), entitybox{200, 100, 50, -50} , render_pointer(&all_trees) {
    AllMapEntities.push_back(this);
    SendToRender();
}```
this causes an unresolved external error. I am not sure if it is because of when it is initialized.

About your tip on reading about containers, I have discovered very useful container types I did not know exist in the reference website.
undone beacon
#

Not unless the mapentity is dynamically allocated

covert cobalt
undone beacon
#

If you're making copies, moving objects, etc. frequently, it'll thrash that common vector of objects