C++ 为什么我不能从派生类的实例访问受保护的成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/967352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:09:41  来源:igfitidea点击:

Why can't I access a protected member from an instance of a derived class?

c++inheritance

提问by RomanM

I haven't done C++ in a while and can't figure out why following doesn't work:

我有一段时间没有使用 C++ 并且无法弄清楚为什么以下不起作用:

class A {
protected:
  int num;
};

class B : public A {
};

main () {
  B * bclass = new B ();
  bclass->num = 1;
}

Compiling this produces:

编译它会产生:

error C2248: 'A::num' : cannot access protected member declared in class 'A'

错误 C2248:“A::num”:无法访问类“A”中声明的受保护成员

Shouldn't protected members be accessible by derived classes?

派生类不应该访问受保护的成员吗?

What am I missing?

我错过了什么?

回答by oscarkuo

yes protected members are accessible by derived classes but you are accessing it in the main() function, which is outside the hierarchy. If you declare a method in the class B and access num it will be fine.

是的,派生类可以访问受保护的成员,但您是在层次结构之外的 main() 函数中访问它。如果您在 B 类中声明一个方法并访问 num 就可以了。

回答by jhufford

Yes, protected members are accessible by the derived class, but only from within the class.

是的,派生类可以访问受保护的成员,但只能从类内部访问。

example:

例子:

#include <iostream>

class A { 
   protected:
   int num;
};

class B : public A {    public:
   void printNum(){
      std::cout << num << std::endl;
   }

};

main () {
   B * bclass = new B ();
   bclass->printNum();
}

will print out the value of num, but numis accessed from within class B. numwould have to be declared public to be able to access it as bclass->num.

将打印出 的值num,但从num类中访问Bnum必须将其声明为 public 才能以bclass->num.

回答by Jim Barnett

It is accessible within the scope of B's functions, but you are attempting to access it in main.

它可以在 B 的函数范围内访问,但您试图在 main 中访问它。

回答by i_am_jorf

But you're not accessing it from the derived class. You're accessing it from main().

但是您不是从派生类访问它。您正在从 main() 访问它。

回答by bsruth

When utilizing a class, there really is no difference between protected and private members. Neither are accessible to anything that utilizes the class.

在使用类时,受保护成员和私有成员之间确实没有区别。任何使用该类的东西都无法访问它们。

class A {
    private: int privateNum;

    protected: int protectedNum;

    public:  int publicNum;

    void SetNumbers(int num) {
        privateNum = num; //valid, private member can be accessed in member function
        protectedNum = num; //valid, protected member can be accessed in member function
    }
};

void main() {
    A classA;
    classA.privateNum = 1; //compile error can't access private member
    classA.protectedNum = 1; //compile error can't access protected member
    classA.publicNum = 1; //this is OK
    classA.SetNumbers(1);  //this sets the members not accessible directly
 }

The difference comes into effect when you inherit from a class with protected members.

当您从具有受保护成员的类继承时,差异就会生效。

class B : public A {
};

All private members of a base class are still private, and will not be accessible to the derived class. The protected members, on the other hand, are accessible to the inherited class, but are still not accessible outside of the inherited class.

基类的所有私有成员仍然是私有的,派生类无法访问。另一方面,受保护的成员可以被继承的类访问,但在继承的类之外仍然无法访问。

class B : public A {
 public:
   void SetBNumbers(int num) {
       privateNum = num; //compile error, privateNum can only be accessed by members of A, not B
       protectedNum = num; //this works, as protected members can be accessed by A and B
   }
};

void main() {
  B classB;
  classB.publicNum = 1; //valid, inherited public is still public
  classB.protectedNum = 1; //compile error, can't access protected member
  classB.privateNum = 1; //compile error, B doesn't know that privateNum exists
  classB.SetBNumbers(1); //this sets the members not accessible directly
}

回答by Mark Ransom

"Protected" means protected from access outside of a member function, or a member function of a derived class. The "main" function isn't a member of either class, but it's trying to directly access the member variable.

“受保护”意味着不受成员函数或派生类的成员函数外部的访问。“main”函数不是任一类的成员,但它试图直接访问成员变量。

回答by g06lin

You are seeing exactly what's expected. Try this - http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/for info on inheritance access specifiers.

您看到的正是预期的结果。试试这个 - http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/有关继承访问说明符的信息。

回答by Pravin.2087

Yes you can not access the protected data members in main function.But you can access the protected data members in main by creating function in Derived call.

是的,您不能访问主函数中受保护的数据成员。但是您可以通过在派生调用中创建函数来访问主函数中的受保护数据成员。