I am trying to make a list of iNUIsensor objects, in c++.
I have tried using this:
std::list<INuiSensor*> nuiList;
...
nuiList.push_front(&nui);
Upon compiling I get this error:
error C2664: 'void std::list<_Ty>::push_front(_Ty &&)' : cannot convert parameter 1 from 'INuiSensor **' to 'INuiSensor *&&'
How can I fix this problem?
Edit:
type of nui:
INuiSensor * nui;
You’re pushing a pointer to a pointer to
nuiwhile the list is expecting a pointer tonui:The fact is that nui is a pointer to begin with, and the operator
&takes the address of the given object, so:Is readed as:
take the address of the object named nui. So, if the object is a pointer toniuthe address of a pointer is a pointer to a pointer.This isn’t asked but I think is worth to say: Could be a good idea to avoid storing object pointers into containers and change the store type by object instances:
When you’re using pointers you must take care of the allocation and deallocation of the objects, if the lists are global you’ll need a public Close method to deallocate all the memory managed by the pointers stored in the lists, if the list is owned by some object, you need to do the same process into the object destructor.
But, if you’re storing object instances, the list destructor deallocates all the objects stored when it’s lifetime ends, the code is cleaner and easier to maintain.