访问派生类中的受保护成员 [英] Accessing protected members in derived class
本文介绍了访问派生类中的受保护成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在CLI/C ++中,派生类可以访问受保护的基类成员吗?
以下代码导致错误C2248
In CLI/C++, can a derived class access protected members of based class?
The following code results in error C2248
class B;
Class A{
protected :
B b;
int m;
};
private class C : A
{
public:
void method();
}
C::method()
{
A::m; // error C2248 : m : cannot access protected members declared
in class A
}
推荐答案
由于受保护,因此可以通过protected
的定义在派生类中对其进行访问.不要写"B->m
",只使用m
.
顺便说一句,您的问题是关于CLI/C ++的,这是我不熟悉的某种语言. :)我知道C ++和C ++/CLI.您的代码是关于C ++的.
您的代码显示9个错误,而您所描述的都不是!
你想骗谁我想,你自己.要准确!
如果您不相信它,请尝试按原样对其进行编译.
由于您的代码"几乎每行都包含一个错误,因此我看不到修复它的原因. 不懂你学到的东西!"
—SA
As they are protected they are accessible in the derived class by definition ofprotected
. Don''t write "B->m
", use justm
.
By the way, your question is about CLI/C++, some language I am not familiar with. :) I know C++ and C++/CLI. You code is about C++.
Your code shows 9 errors, none of then is the one you describe!
Who are you trying to fool? I think, yourself. Be accurate!
If you don''t believe it, try to compile it as is.
As your "code" contains a bug in almost every line, I cannot see a reason to fix it. "Unlearn what you have learned!"
—SA
正如SA所说,您的代码未使用任何C ++/CLI,并且粘贴的内容甚至都不会编译.这是将要编译的固定版本,尽管它与C ++/CLI无关:
As SA said, your code did not use any C++/CLI and what you pasted will not even compile. Here''s the fixed version that will compile, although it has nothing to do with C++/CLI:
class B{};
class A
{
protected :
B b;
int m;
};
private class C : A
{
public:
void method();
};
void C::method()
{
A::m;
}
这篇关于访问派生类中的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文