c++访问静态类的成员变量,不是友
C++ access to static class member variables, not friend
如果这是一个愚蠢或无意义的问题,我很抱歉,但是:
一个类的非常量静态类变量可以在不使用友类或基类/派生类的情况下被另一个类使用吗?(缩写)情况如下:
class Decl {
public:
static string searchVal;
... (other irrelevant stuff)
};
class Conj {
public:
static string searchVal;
... (other irrelevant stuff)
};
我不想在两个类中重复searchVal,并且由于程序的其余部分,我不热衷于使用friend(但如果它是唯一的选择,我会使用它)。
因为你的static
成员是public
,如果你的类定义都是可见的,那么它们的静态成员可以分别使用Decl::searchVal
或Conj::searchVal
访问。
class Decl
{
public: static string searchVal;
};
class Conj
{
public:
static string searchVal;
};
// within ANY function, including members of either class above
// ... as long as both definitions above are visible to the compiler
if (Conj::searchVal == Decl::searchVal)
{
// do something
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- C++预处理会生成变量成员、资源库和映射
- 局部堆栈变量成员的返回值优化
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 使用指向结构变量成员的指针访问该结构的成员的地址
- 如果派生类仅包含自动变量成员,是否有必要具有虚拟驱动器
- 线程安全性和静态变量/成员功能
- "static const char array"可以在 C 语言上包含变量成员吗
- 仅用于内部目的的类的所有变量/成员的技术术语是什么
- 如何强制转换变量成员以将其作为函数的引用参数传递
- 类中未声明变量成员函数
- 在 main 中初始化类的 "static const" 类型变量成员的更好方法
- c++模板类静态const变量成员作为映射键给出未定义引用
- 在类中初始化结构变量成员会导致分割错误
- 在c++中建模变量成员类型
- 模板私有静态变量成员的未定义符号
- 不能访问公共静态变量成员
- 常量变量成员在C++有什么用?
- g++ 4.8.2坚持简单变量成员是数组