1、公有静态成员函数不能直接访问普通私有成员,因为它没有关联的对象实例,没有this指针。
2、如果在该公有静态函数中可以获取到对象的指针,则可以通过该指针访问该对象的私有成员。因为该静态函数是公有的成员函数。
具体代码样例如下中的 callback函数:
#include#includeclass user{
public:
static user* instance()
{
if(myself == nullptr)
{
myself = new user();
}
return myself;
}
static int callback()
{
return user::instance()->age;
}
user():age(18){};
private:
int age;
static user* myself;
};
user* user::myself = nullptr;
int main()
{
printf("age:%d\n", user::instance()->callback());
return 0;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧