[英]Can an inherited static function access an overridden static private data member?
I am wondering about the following: 我想知道以下几点:
In static.h: 在static.h中:
class Base
{
private: static const char* className;
public: static const char* getClass() { return className; };
};
class Sub : public Base
{
private: const static char* className;
};
And in static.cpp: 并在static.cpp中:
#include "static.h"
#include <iostream>
const char* Base::className = "Base";
const char* Sub ::className = "Sub";
int main() { std::cout << Sub::getClass(); }
// outputs "Base"
Is it possible to inherit such a function and have it use an overridden data member? 是否可以继承这样的功能并使用重写的数据成员?
Yes, but this is not 'overriding', it is actually 'hiding'. 是的,但这不是“覆盖”,实际上是“隐藏”。 The class can access its own private members, and its data members hide declarations using the same name in base classes. 该类可以访问其自己的私有成员,并且其数据成员在基类中使用相同的名称隐藏声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.