Hynek Blaha
Hynek Blaha

Reputation: 653

Signal handling in C++ (using instance variable inside method)

I'm not very skilled in C++, so I find this tricky.

I have a class which looks pretty similar to this one : Is it possible to use signal inside a C++ class?., but inside the signal handling method I have to work with an instance variable.

The first thing I tried was to set the variable static, but it didn't help. What should i do? Thanks Hynek

Upvotes: 0

Views: 711

Answers (1)

Brady
Brady

Reputation: 10357

If you want to work with an instance variable, then you'll need a class instance, since you cant access instance variables from a static method. Your next question will be how to get the class instance, for which there are a few options. You can either make a global variable, which is usually frowned upon, or you could consider making a Singleton. The approach used in option 2 of the accepted answer to the question you referenced is similar to a singleton, so that could also be an option.

Perhaps this would be clearer if you reviewed static methods and attributes. Bruce Eckel's Thinking in C++ is an excellent free online c++ book that should help.

Upvotes: 1

Related Questions