struct FireData
{
//stuff
const c8* filename;
}
//in the main function:
{
FireData fire = new FireData();
//reading a xml-file
{
if (stringw("fire20") == mapXml->getNodeName())
{
fire->file = mapXml->getAttributeValue("file");
}
}
//Loading a fireemitter
//Loading the texture for the emitter:
fire->node->setMaterialTexture(0, driver->getTexture(filename));
//do the rest
}
The problem is: when the fireemitter is loaded and I try to load the texture, the const c8* string in fire->file is not valid anymore.
I think I know how to solve the problem: by simply using a strcpy-like funktion, which copy's from const char* to const char*
I'm taking a pitch at this, even though I'm no expert in keywords like const
As far as I know, a const variables' value can be defined before compile, but if it's to be changed at runtime, I only think it can reference another variable. That might have to do with why it gets lost.
Err, this is not named 'const' without intentions. It's simply constant so you should not be able to change it. Const pointers are usually used as parameters showing that the values pointed to are not changed by the method. If you really want to use c8* you have to use strncpy or even better strdup to create the memory on the fly. Otherwise use core::stringc.
I have to keep the const c8*, because I'm reading a texture-filepath from the X-File and load that file directly by using getTexture (see my code-example)
You don't have to use a const c8* because every c8* can become const c8*. This conversion is done autmatically, e.g. when passing this pointer to a const param method. The other way round is not possible without explicitly casting. And you can easily get a const c8* from a core::stringc with the .c_str() method.
hybrid wrote:You don't have to use a const c8* because every c8* can become const c8*. This conversion is done autmatically, e.g. when passing this pointer to a const param method.
Intresting, it was the first thing I tried, and the compiler gave me the error "cannot convert from const c8 * to c8 *"...
hybrid wrote:And you can easily get a const c8* from a core::stringc with the .c_str() method.
hybrid wrote:You don't have to use a const c8* because every c8* can become const c8*. This conversion is done autmatically, e.g. when passing this pointer to a const param method.
Intresting, it was the first thing I tried, and the compiler gave me the error "cannot convert from const c8 * to c8 *"...
No, you cannot convert from const to non-const, only the other way round. This is pretty forward if you take the intention mentioned some posts before (jus marking some parameters to be unchanged). If you pass a non-const variable to a const parameter it's ok. But if you would pass a const variable to a non-const parameter chances are good that the method would try to alter the const variable. And that's obviously an error.