Set&Get userdata (Pointer problem)

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

Eralp
Just popping in
Just popping in
Posts: 10
Joined: Fri Apr 24, 2009 17:23

Set&Get userdata (Pointer problem)

Postby Eralp » Mon Jan 24, 2011 23:12

Hello, I have been dealing with this problem nearly like 2 hours now and it is irritating -.-

I minimalized the code here. newitem is a base class(baseItem class).

healthPotion <=== consumable <===== baseItem

my consumable class has a pure virtual function that is overriden in healthPotion class ( if that matters)

item is just a CEGUI window which I use for storing data in this code.

Code: Select all

healthPotion* omg = (healthPotion*)newitem;
                //I can directly cast newitem to healthpotion and I can call virtual function, so the cast is correct.
                the memory location of omg = 0x0aa8c480

                item->setUserData(newitem);
                        omg =  (healthPotion*)item->getUserData();
                //But when I set the userdata of the cegui::Window( which accepts a void) and then cast it back to healthPotion. I CAN'T use the virtual function and the cast is not correct.
                the memory location of omg = 0x0aa8c484


Doesnt CEGUI::Window just store the pointer without changing? Or am I missing something about pointers and type-casting? or maybe virtual functions I have in consumable class
And the location of pointers change if I set and get the user data but shouldn't they stay the same?

User avatar
Kulik
CEGUI Team
Posts: 1382
Joined: Mon Jul 26, 2010 18:47
Location: Czech Republic
Contact:

Re: Set&Get userdata (Pointer problem)

Postby Kulik » Tue Jan 25, 2011 00:19

I suggest you to stop using C-style casts and learn fancy C++ casts instead - static_cast, dynamic_cast and reinterpret_cast.

CEGUI definitely doesn't change the pointer. The reason why you can't cast isn't really apparent without compiler error logs and the whole code but watch out for constness!

Code: Select all

void* ptr = 0;
reinterpret_cast<healthPotion*>(ptr)->drink();


This will definitely work and let you crash the app...

Eralp
Just popping in
Just popping in
Posts: 10
Joined: Fri Apr 24, 2009 17:23

Re: Set&Get userdata (Pointer problem)

Postby Eralp » Tue Jan 25, 2011 00:51

Thanks for the response, I tried the reinterpret and static cast too but I had no luck.

And the same pointer works before setting and getting it from the window.

I tried to delete the virtual function and put it on healthPotion class and casted that pointer directly to healthPotion and I can now use the consume function without problem. But I want to cast to consumable object and then call the virtual function which will make my life easier later..

Here is the consumable class

Code: Select all

class consumable : public baseItem
{
public:
   consumable(int id) : baseItem(id)
   {
      typeID = consumableID;
   }

   virtual void onConsume(player* who) =0;
};

//and healthPotion class, consume does nothing for simplicity..

class healthPotion : public consumable
{
public:
healthPotion(int id) : consumable(id)
{
   itemID = healthPotionID;
}

virtual void onConsume(player* who) {}

};


I can't decide what I am doing wrong because I can cast baseItems and use with no problem except the ones I get from getUserData().

Eralp
Just popping in
Just popping in
Posts: 10
Joined: Fri Apr 24, 2009 17:23

Re: Set&Get userdata (Pointer problem)

Postby Eralp » Tue Jan 25, 2011 01:25

Problem solved.Followings are just my ideas I am not sure if they are correct..

It seems that the compiler can not climb up the hierarchy if the given pointer is void. And It seems that dynamic_cast doesn't work on void pointers so I tried that:

consumable* consumeitem = dynamic_cast<consumable*>(reinterpret_cast<baseItem*>(mouseArgs.window->getUserData()));

And I had to include at least one virtual function to very base class. So I added a virtual destructor and It worked :)


Return to “Help”

Who is online

Users browsing this forum: No registered users and 18 guests