简体   繁体   English

C ++中的信号处理(在方法内部使用实例变量)

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

I'm not very skilled in C++, so I find this tricky. 我不太熟练C ++,所以我觉得很棘手。

I have a class which looks pretty similar to this one : Is it possible to use signal inside a C++ class? 我有一个看起来非常类似于该类的类: 是否可以在C ++类中使用signal? ., 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. 我尝试的第一件事是将变量设置为static,但没有帮助。 What should i do? 我该怎么办? Thanks Hynek 谢谢海尼克

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 . 您可以创建一个通常不赞成使用的全局变量,也可以考虑创建一个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. 您所引用问题的可接受答案的选项2中使用的方法类似于单例,因此也可以选择。

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. 布鲁斯·埃克尔(Bruce Eckel)的C ++思维(Thinking in C ++)是一本很好的免费在线c ++书,应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM