静态关键字在这里有帮助吗?
Will static keyword helps here
class A{
int _a;
public:
/--/ void setfunc(int a) ............. will static works here
{
_a=a;
}
int getValue(){return _a};
};
class B{
public:
void func()
{
/--/ setfunc(1); ...................Dont want to create object of A.
}
};
class C{
public:
void something()
{
A aa;
cout<<aa.getValue(); ............. want a value update by class B setfunc
}
};
int main()
{
B bb;
bb.func();
C cc;
cc.something();
}
问题:setfunc()如何在另一个函数中调用而不使用该类对象?此外,如果它改变了,比如通过某些类b设置"_a"的值,当我试图通过getValue()
在静态函数中只能使用类的静态成员。像这样(_a是静态的):
class A {
static int _a;
public:
static void setfunc(int a)
{
_a=a;
}
static int getValue(){return _a};
};
否则,你不能对非静态成员做任何事情:
class A {
int _a;
public:
static void setfunc(int a)
{
_a=a; // Error!
}
static int getValue(){return _a}; // Error!
};
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 有可能在信号处理程序中设置promise吗
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如果 x.h 仅由函数声明组成,为什么有必要在 x 中包含 x.h.cpp
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 为什么thread_local变量在这里从未初始化?
- 为什么我必须在这里使用dynamic_cast
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 有哪些好的做法可以帮助在未定义的映射和矢量中预防std::bad_alloc
- 我无法弄清楚此访问冲突错误.有人可以在这里帮助我吗?
- 我在这里使用"cin.get"有什么问题?
- "类"在这里有一个先前的声明
- 我想显示序列 ftom 16 到 31 十进制数,但它没有显示:谁能在这里帮助我
- 在这里"boost::mpl::identity<T>::type"有什么意义?
- 静态关键字在这里有帮助吗?
- 这是一个普遍的参考吗?std::forward在这里有意义吗?