sceneNode->getName, how can I get rid of the const

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
nayon
Posts: 16
Joined: Mon Mar 16, 2009 1:07 pm

sceneNode->getName, how can I get rid of the const

Post by nayon »

Hello everyone, I'm trying to get the name of a sceneNode and store it into a variable of type c8* for later usage. But I can't seem to do it. What I do is:

Code: Select all

c8* name;
strcpy(name,const_cast<irr::c8*> (selectedSceneNode->getName()));

I tried separating it into steps, but the error happens at the const_cast. A memory location error. How can I solve this, is there an easy way?
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

why don't you use const c8* ?
Then you could do

Code: Select all

const c8* name = node->getName();
Or you could simply use the string class
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
nayon
Posts: 16
Joined: Mon Mar 16, 2009 1:07 pm

Post by nayon »

I don't want to use const because I will be changing the value of the variable "name", which I can't do with consts.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Well then use core::string class
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Re: sceneNode->getName, how can I get rid of the const

Post by vitek »

nayon wrote:Hello everyone, I'm trying to get the name of a sceneNode and store it into a variable of type c8* for later usage. But I can't seem to do it. What I do is:

Code: Select all

c8* name;
strcpy(name,const_cast<irr::c8*> (selectedSceneNode->getName()));

I tried separating it into steps, but the error happens at the const_cast. A memory location error. How can I solve this, is there an easy way?
Note that the second parameter in the signature of strcpy is const char* (which is equivalent to const c8*)...

Code: Select all

char *strcpy(char *s1, const char *s2);
So the cast you are using is unnecessary. The problem is that you don't appear to understand how to use strcpy(). The first parameter is supposed to be a pointer to a sufficiently large buffer of characters, not an uninitialized pointer. Something like this...

Code: Select all

char name [32]; // assumes all names are less than 31 characters
strcpy (name, node->getName ());
// now you can modify the contents of the name buffer
It would be slightly more safe to use strncpy() or strdup(). As pointed out above, you could just use core::stringc to do what you need...

Code: Select all

core::stringc name = node->getName ();
// modify name using methods of the string class
Travis
nayon
Posts: 16
Joined: Mon Mar 16, 2009 1:07 pm

Post by nayon »

Thank you. And R.I.P Vitek if your name refers to the Vitek I'm thinking about.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Actually it is my name (my last name), but I do know who you are talking about (the drummer for Decapitated).

Travis
Post Reply