类的静态成员变量
类的静态成员变量
coding时遇到一个需求,抽象出来大概意思就是:A类的一个静态成员变量,需要在B类里面赋值,然后在C类里面使用。知识点:类的静态成员变量需要在全局域进行申明,可以将声明和赋值放一起,也可以分开。
#include<iostream>
class A
{
public:
static double DistanceTh_ ;
};
double A::DistanceTh_ ;
class B
{
public:
B();
};
B::B(){
A::DistanceTh_ = 2.0;
}
class C
{
public:
int x = A::DistanceTh_;
};
int main()
{
B b = B();
C c = C();
std::cout<<"A::DistanceTh_ = "<<A::DistanceTh_<<std::endl;
std::cout<<"C.x = "<<c.x<<std::endl;
return 0;
}
运行结果:
A::DistanceTh_ = 2
C.x = 2
还没有评论,来说两句吧...