你能在成员函数中创建一个静态变量吗?这个变量在该类的实例中是静态的
Can you create a static variable within a member function, that is only static within an instance of that class?
我有点厌倦了总是为一个类创建一个memeber,而这个类最终只会在一个作为计数器的memeber函数中使用一次
我想知道是否有一种方法可以在类中声明一个变量,该变量仅对单个实例是静态的,下一个实例再次以0开头
因此,我基本上要寻找的是单个函数中类成员的快捷方式
我认为答案是一个非静态成员变量,但我可能误解了你的问题。
class Example
{
private:
int mCounter;
public:
Example() :mCounter(0)
{
// Empty
}
int increment()
{
mCounter++;
return mCounter;
}
};
相关文章:
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 模板基类中的静态变量
- 类和静态变量
- 不同作用域中的静态变量和全局变量
- 静态变量声明和定义
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 在类中继承静态变量?
- "local scope"中的 C++ 初始化静态变量
- 使用静态变量的递归调用的不同输出
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 复制文件流C++静态变量
- 如何在复杂继承中访问静态成员变量
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 跨模板化函数编译的静态变量
- constexpr函数中的静态constexpr变量
- 静态成员变量不会由 gettext 转换
- 从 exe 和 dll 访问静态库中的 extern 变量
- 如果包含映射的静态库与可执行文件和动态库链接,静态映射(变量)是否会被多次释放?
- 用于定义公共变量静态常量整数的C++标准