Access global object in call of function pointer

Kasperdelasopa
Posts: 7
Joined: Fri Mar 29, 2019 10:02 am

Access global object in call of function pointer

Postby Kasperdelasopa » Fri Mar 29, 2019 10:20 am

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

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Access global object in call of function pointer

Postby idahowalker » Fri Mar 29, 2019 9:11 pm

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.

Kasperdelasopa
Posts: 7
Joined: Fri Mar 29, 2019 10:02 am

Re: Access global object in call of function pointer

Postby Kasperdelasopa » Sun Mar 31, 2019 9:00 am

Actually i could solve it by just using std::string instead of const char* all over the place

But Thanks

Who is online

Users browsing this forum: No registered users and 68 guests