C++:如何从静态函数写入全局变量
C++: How to write from a static function to a global variable
我想访问一个变量(通过ctypes),它应该是静态void函数的结果,即监听广播。
那么,除了使用 "return" 语句之外,我如何从静态函数中获取信息呢?
编辑:
这是我的意思的示例代码:
class Foo{
int bar;
static void listener(){
bar = 3;
}
main(){
listener();
}
}
静态方法只能访问静态成员:
class Foo {
static int bar;
public:
static void listener() { bar = 3; }
};
int Foo::bar = 0;
int main()
{
Foo::listener();
}
您可以将变量声明为 static
:
class Foo {
public:
static int bar;
static void listener() {
bar = 3;
}
};
int Foo::bar = 0;
main() {
Foo::listener();
}
// now use Foo::bar. It's value is 3.
}
请注意,正在public
和static
的变量可以由每个人修改,而不仅仅是通过Foo::listener
相关文章:
- 内联函数中具有内部链接的全局变量
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 不同作用域中的静态变量和全局变量
- C++ 在编译时具有函数计算全局变量
- 修改程序的入口点时未调用全局变量的构造函数
- 赋予全局变量而不是局部变量优先级的函数 - (异常行为)
- std::线程不是全局变量,但在到达创建它的函数的末尾时不会超出范围?
- 静态 constexpr 全局变量
- 如何创建用于多个源文件的全局变量/包含/函数
- 通过使用全局变量的函数访问在给定范围内被覆盖的变量
- C++:如何从静态函数写入全局变量
- 可以静态访问全局静态变量的静态函数
- 递归计算在不使用静态局部变量,全局变量或静态函数的情况下,在递归函数中发生的环数
- 访问内联函数中的静态全局变量
- 全局变量构造函数/析构函数是否需要线程保护
- 当到达main()时,由全局变量的构造函数初始化的静态模板变量为空
- c++全局变量和函数
- 用于初始化全局变量的函数究竟何时执行
- 代码清理-删除c++中无用的文件,包括,全局变量和函数
- 头文件中的全局变量和函数