[C++-sig] operator in return false while operator == return true
simwarg at gmail.com
Fri Sep 24 23:42:35 CEST 2010
Hey, sorry for the lacking information.
May I declare it as this instead:
map<GameObject*, Data> a_map; // this map has "nothing" to do with
Gameobject* obj = new Gameobject(id);
a_map[ obj ] = ...some data...
// get game object from map
obj = a_map.getgameobject()
}; // end of class
The python class:
def __init(self, the_c++_GameobjectManager_pointer):
self.manager = the_c++_GameobjectManager_pointer
gameobject = self.manager.createGameObject(1)
#self.manager.createGameObject() returns a pointer to the gameobject created
self.mylist[gameobject] = ..some data..
def callPython(self, gameobj): # at some point, c++ will call this
function and pass the game object we created above with id 1
for x in self.mylist.keys():
print(x == gameobj) # True
print(gameobj in mylist) # False
So basically the c++ class and python class have their own dict but I want
them to have the *same *address value in their keys .. is it possible??
It seems, in c++ the key is the address of the game object and in Python it
some other address (but they basically point to the same object/memory). Is
there any way around this *without* changing the type of the key being used,
i.e. keep the key as a pointer.
On Fri, Sep 24, 2010 at 4:27 PM, Stefan Seefeld <seefeld at sympatico.ca>wrote:
> I don't quite understand what you are trying to do. Please provide a little
> more detail. Your current mail requires far too much second-guessing to be
> On 09/24/2010 10:08 AM, Simon W wrote:
>> Im really scared because I fear a fundamental issue concerning the script
>> system in my game engine.
>> In C++, I have a class. In my class I map data to GameObject like:
>> map<GameObject*, Data> .....
> OK. (Naming this class would help the discussion, though.)
>> As you see, I use the pointer as key.
>> When I from c++, in the same class, call a python function I pass the key
>> like this:
>> // the variable /gameobj /is of type GameObject*
>> When I recieve the call in python I do some checks like this:
>> *def callbackFunction(self, gameobj):
>> for x in self.mydict.keys():
>> print(gameobj == self.mydict)
>> print(gameobj in self.mydict)*
> This looks wrong. You iterate over 'x', but don't use it in the loop. May I
> assume that 'mydict' relates to the above map<GameObject*, Data> in the
> unnamed class ?
>> The above will print something like:
> This suggests that 'gameobj' compares equal to the 'mydict' object, but
> that it is not itself included in the sequence returned by mydict.keys().
>> I do have a overloaded == operator. But how can I fix so that python
>> checks for my pointer and not the PyObject* pointer ?
> What type do you consider providing an operator== for ?
> ...ich hab' noch einen Koffer in Berlin...
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Cplusplus-sig