简体   繁体   English

继承的静态函数可以访问重写的静态私有数据成员吗?

[英]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.

相关问题 如何访问从模板类继承的私有静态类成员? - How to access private static class member inherited from a template class? 可以派生类访问私有静态成员函数 - Can derived class access private static member function 从静态助手功能访问私有成员 - Access to private member from static helper function 静态成员函数如何调用静态私有数据成员? 需要什么样的内部转换? - How static member function can call the static private data member ? what kind of the internal transformation is needed? 如何在另一个类的静态成员函数中访问私有静态变量? - How to access private static variable in static member function of another class? 静态成员函数访问静态私有变量时链接器错误 - Linker error when static member function access static private variable 在静态成员初始化期间访问私有静态函数 - Access to private static function during static member initialization 非静态成员函数可以访问静态成员函数或数据吗? - Can non-static member function access static member function or data? 使用静态成员函数访问静态数据成员 - Using Static Member Function to Access Static Data Member C ++:公共静态成员函数如何访问私有实例成员变量? - C++: How can a public static member function access private instance member variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM