Page 1 of 1

Access global object in call of function pointer

Posted: Fri Mar 29, 2019 10:20 am
by Kasperdelasopa
Hello,

i'm using function pointers to dynamically call functions.

I have created a global object to store some settings in it.

Code: Select all

class Settings{
	public:
			Settings();
			std::string getID();
	private:
			std::string ID;		
};
I define a object like this globally :

Code: Select all

Settings *_settings;

....

setup(){
_settings = new Settings();

Serial.println(_settings->getID().c_str()); //PointA
}

...

void functionCalledFromPointer(){

Serial.println(_settings->getID().c_str()); //PointB

}
At PointA the content is printd correctly.

At PointB the content is emtpy.

What can i do to have the global objetc correctly accessable?

Best regards

Re: Access global object in call of function pointer

Posted: Fri Mar 29, 2019 9:11 pm
by idahowalker
Perhaps change this:

Code: Select all

Settings *_settings;
....

setup(){
_settings = new Settings();
to this:

Code: Select all

Settings *_settings;

_settings = new Settings();
....

setup(){
Serial.println(_settings->getID().c_str()); //PointA

…………..

void functionCalledFromPointer(){

Serial.println(_settings->getID().c_str()); //PointB

}




When the object is created inside of setup the objects scope is defined within setup. When the object is defined global, the object is accessible global.

Re: Access global object in call of function pointer

Posted: Sun Mar 31, 2019 9:00 am
by Kasperdelasopa
Actually i could solve it by just using std::string instead of const char* all over the place

But Thanks