为什么我不能访问作为参数传递给函数的基类的受保护成员变量? [英] Why can't I access a protected member variable of a base class passed into a function as an argument?
问题描述
这个答案似乎表明它应该有效,为什么我的例子会出现编译错误:
This answer seems to suggest it should work so why does my example kick up a compiler error:
class Class1
{
protected:
long m_memberVar;
};
class SubClass1: public Class1
{
public:
void PrintMember(Class1 memberToPrintFrom)
{
Console::Write("{0}", memberToPrintFrom.m_memberVar); // <-- Compiler error: error C2248: 'BaseClassMemberAccess::Class1::m_memberVar' : cannot access protected member declared in class 'BaseClassMemberAccess::Class1'
}
};
- 在Need4Sleep的建议中将子类更改为公共继承,但没有区别。
推荐答案
在这个答案中,我假设您使用了 public
代码中的继承(问题中缺少)。
In this answer I'll assume that you used public
inheritance in your code (which was missing from the question).
[C ++ 11:11.2 / 1]:
如果使用<$ c $将一个类声明为另一个类的基类(第10条) c> public 访问说明符,基类的public
成员可以public $访问c $ c>派生类的成员和
protected
基类成员可以作为protected
成员访问派生类。如果使用protected
访问说明符将一个类声明为另一个类的基类,那么public
和protected
基类的成员可以作为派生类的protected
成员访问。如果使用private
访问说明符将一个类声明为另一个类的基类,那么public和protected
成员基类的可以作为private
派生类的成员访问。
[C++11: 11.2/1]:
If a class is declared to be a base class (Clause 10) for another class using thepublic
access specifier, thepublic
members of the base class are accessible aspublic
members of the derived class andprotected
members of the base class are accessible asprotected
members of the derived class. If a class is declared to be a base class for another class using theprotected
access specifier, thepublic
andprotected
members of the base class are accessible asprotected
members of the derived class. If a class is declared to be a base class for another class using theprivate
access specifier, the public andprotected
members of the base class are accessible asprivate
members of the derived class.
这包括您正在访问相同对象的成员的情况。
This covers the case where you're accessing a member of the same object.
但是,它有点好奇 protected
成员访问权限为了访问另一个对象的 protected
成员,它必须位于相同类型的定义或更多派生的类型;在你的情况下,它是一个较少派生的类型(即基数):
However, it's a little curiosity of protected
member access that in order to access a protected
member of another object, it has to be located within the definition of the same type or a more derived type; in your case, it is in a less-derived type (i.e. a base):
[C ++ 11:11.4 / 1]:
当非静态数据成员或非静态成员函数是其命名类的受保护成员时,将应用超出前面第11章所述的附加访问权限检查(11.2) )如前所述,授予对受保护成员的访问权限,因为引用发生在某个类C
的朋友或成员中。如果访问要形成指向成员的指针(5.3.1),则嵌套名称说明符应表示C
或派生自的类C
。所有其他访问都涉及(可能是隐式的)对象表达式(5.2.5)。 在这种情况下,对象表达式的类应为C
或从C
派生的类。
[C++11: 11.4/1]:
An additional access check beyond those described earlier in Clause 11 is applied when a non-static data member or non-static member function is a protected member of its naming class (11.2) As described earlier, access to a protected member is granted because the reference occurs in a friend or member of some classC
. If the access is to form a pointer to member (5.3.1), the nested-name-specifier shall denoteC
or a class derived fromC
. All other accesses involve a (possibly implicit) object expression (5.2.5). In this case, the class of the object expression shall beC
or a class derived fromC
.
也就是说,您必须在 Class1 $中运行此代码c $ c>成员函数。
That is, you'd have to run this code from within a Class1
member function.
Bjarne在他的书中提到了这一点 The C ++ Programming Language(Sp.Ed。),第404页:
Bjarne mentions this in his book The C++ Programming Language (Sp. Ed.) on page 404:
派生类只能为自己类型的对象访问基类'受保护成员[...]这样可以防止出现细微的错误否则在一个派生类破坏属于其他派生类的数据时发生。
A derived class can access a base class' protected members only for objects of its own type [...] This prevents subtle errors that would otherwise occur when one derived class corrupts data belonging to other derived classes.
这篇关于为什么我不能访问作为参数传递给函数的基类的受保护成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!