IGUIElement collection

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
barakatx2
Posts: 14
Joined: Tue Aug 24, 2010 7:00 am

IGUIElement collection

Post by barakatx2 »

I have an std::map that contains IGUIElements. I use this to track all my GUI elements. Lets say I add an IGUIEditBox to this map. When I try this:

controlList[id].setMultiLine(true);

This will not work because IGUIElement does not have this function. So I try to cast it as IGUIEditBox:

(gui::IGUIEditBox)controlList[id].setMultiLine(true);

And it says you can not cast with an abstract class. Can I not have edit boxes in an std collection class like this? Any ideas would be appreciated.
TomTim
Posts: 72
Joined: Mon Aug 16, 2010 5:32 pm
Location: NRW, Germany

Re: IGUIElement collection

Post by TomTim »

barakatx2 wrote:I have an std::map that contains IGUIElements. I use this to track all my GUI elements. Lets say I add an IGUIEditBox to this map. When I try this:

controlList[id].setMultiLine(true);

This will not work because IGUIElement does not have this function. So I try to cast it as IGUIEditBox:

(gui::IGUIEditBox)controlList[id].setMultiLine(true);

And it says you can not cast with an abstract class. Can I not have edit boxes in an std collection class like this? Any ideas would be appreciated.
Save the element in a variable:

Code: Select all

irr::gui::IGUIEditBox*EditBox=dynamic_cast<irr::gui::IGUIEditBox*>(controlList[id]);
EditBox->setMultiLine(true);
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: IGUIElement collection

Post by Acki »

barakatx2 wrote:(gui::IGUIEditBox)controlList[id].setMultiLine(true);
the "casting-braces" are wrong !!!
you're now casting the return value of setMultiLine(true) (which has no return value) !!! :lol:
try this:

Code: Select all

((gui::IGUIEditBox)controlList[id]).setMultiLine(true)
also are you sure to use the dot operator ??? :shock:
should it not be:

Code: Select all

((gui::IGUIEditBox)controlList[id])->setMultiLine(true)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
TomTim
Posts: 72
Joined: Mon Aug 16, 2010 5:32 pm
Location: NRW, Germany

Re: IGUIElement collection

Post by TomTim »

Acki wrote:also are you sure to use the dot operator ??? :shock:
should it not be:

Code: Select all

((gui::IGUIEditBox)controlList[id])->setMultiLine(true)
This isn't right, you should cast to a pointer to a IGUIEditBox, not to a IGUIEditBox object ...

@barakatx2:

Here some advices:

1. Don't save Irrlicht objects in a container, save them as pointers to Irrlicht objects.

Code: Select all

#include <vector>

std::vector<IGUIElement*> GUIVector;
Otherwise you'll have to use the adress operator (&) a lot, for Irrlicht uses a lot of pointer.

2. If you wanna cast a basic type to a diverted type (hope the idiom is correct), use a dynamic_cast.

Code: Select all

#include <vector>
class Base{};

class Diverted:Base
{
public:
    void SomeFunctionBaseDoesNotHave(){}
};

std::vector<Base*> BaseVector;
Base*PointerToBase=new Diverted();
//PointerToBase->SomeFunctionBaseDoesNotHave(); //The object itself is diverted, but the pointer is not, so you can't call the function.
BaseList.push_back(PointerToBase); //Now how to get SomeFunctionBaseDoesNotHave?
Diverted*PointerToDiverted=dynamic_cast<Diverted*>(BaseList.at(0)); //Get first item of the vector, cast it to diverted type and save it.
PointerToDiverted->SomeFunctionBaseDoesNotHave(); //OK.
3. DON'T mix up pointers with objects. If you use a pointer without the '*', it's just an adress (__int32 or __int64, depends on your OS), no object. And an integer will not have any function at all.

Code: Select all

//Imagine that the used constructors exist.
IGUIEditBox Object;
IGUIEditBox*Pointer=new IGUIEditBox();
Object.getText();//OK.
Pointer->getText();//Also OK.
Object->getText();Error, Object is not able to be casted into adress.
Pointer.getText();Error, the adress is just a number, no IGUIEditBox object.
Hope you see it.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Please do not use dynamic_cast. Irrlicht is compiled without rtti, so that won't work. Use c-casts or static_cast instead.
And your main problem is that you try to cast objects instead of casting pointers. Work with pointers instead.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
TomTim
Posts: 72
Joined: Mon Aug 16, 2010 5:32 pm
Location: NRW, Germany

Post by TomTim »

CuteAlien wrote:Please do not use dynamic_cast. Irrlicht is compiled without rtti, so that won't work.
So I understand why some of my dynamic_casts haven't worked. OK, ignore what I wrote.
barakatx2
Posts: 14
Joined: Tue Aug 24, 2010 7:00 am

Re: IGUIElement collection

Post by barakatx2 »

Acki wrote:
barakatx2 wrote:(gui::IGUIEditBox)controlList[id].setMultiLine(true);
the "casting-braces" are wrong !!!
you're now casting the return value of setMultiLine(true) (which has no return value) !!! :lol:
try this:

Code: Select all

((gui::IGUIEditBox)controlList[id]).setMultiLine(true)
also are you sure to use the dot operator ??? :shock:
should it not be:

Code: Select all

((gui::IGUIEditBox)controlList[id])->setMultiLine(true)
This was right. Also I am using pointers, I had just put a period incorrectly in the code I pasted for some reason @_@. But ((gui::IGUIEditbox*)controlList[id])->setMultiLine(true); works. Thank you!
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: IGUIElement collection

Post by Acki »

TomTim wrote:
Acki wrote:also are you sure to use the dot operator ??? :shock:
should it not be:

Code: Select all

((gui::IGUIEditBox)controlList[id])->setMultiLine(true)
This isn't right, you should cast to a pointer to a IGUIEditBox, not to a IGUIEditBox object ...
yeah, sorry, just a typo, I forgot the asterisk... :oops:

as barakatx2 already figured out it must be:

Code: Select all

((gui::IGUIEditbox*)controlList[id])->setMultiLine(true);
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply