c++ - const char* in my class has junk character(s) after it's returned from function -
class:
class myclass { public: myclass(void); const char* server; private: char pidchar[6]; int pidnum; }; the function
myclass parseini(const char* file) { myclass inioptions; csimpleinia ini; ini.setunicode(); ini.loadfile(file); const char* server = ini.getvalue("", "server", ""); inioptions.server = server; std::cout << server << "\n"; // prints correct value here fflush(stdout); return inioptions; } calling main function
int _tmain(int argc, tchar* argv[]) { myclass options; options = parseini("myapp.ini"); std::cout << options.server << "\n"; // prints junk here return 0; } what did wrong?
the const char* returned getvalue() belonged ini object. when exited parseini() function, ini went out of scope , destroyed, mean pointer no longer valid.
try using std::string server member type instead of const char*.
Comments
Post a Comment