const char* getOutPath()
{
return classVarStr.c_str();
}
I have the previous function,
there is something weird when I receive the returned value,
I get the full path but not including the first character!
so if the path is results/appName_subStr.dat
I get esults/appName_subStr.dat
!
I changed the function call to
string getOutPath()
{
return classVarStr;
}
then I call c_str()
after receiving the value to get the path correct with the first char
I was guessing that it could be happening because of the function stack pop could have modified the address somehow?
anyone faced similar problem, and what could have been the cause?
EDIT:
class X
{
private:
string classVarStr;
public:
X(string in) : classVarStr(in)
const char* getOutPath()
{
return classVarStr.c_str();
}
string getOutPathStr()
{
return classVarStr;
}
}
class B
{
private:
X xinstance;
public:
B(int argc, char * argv[])
{
getSomepathFn(argc, argv);
}
string getAppPath1()
{
return xinstance.getOutPath(); // this create a string then pass a copy, and internally deleted
}
const char * getAppPath2()
{
return xinstance.getOutPathStr().c_str();// this is a problem, create a string, then pass const to the data, and deleted before the function call return, **Undefined behaviour** because the `getOutPathStr()` doesnt return a const reference
}
}
class appObj
{
void printMessage()
{
B obj = getBObj();
FILE *fileptr = fopen(obj->getAppPath2(), "a");// this is the faulty area
}
};