C++ 访问派生类中的基类成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5153696/
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
Accessing a base class member in derived class
提问by Vijay
I have a simple class as below
我有一个简单的类如下
class A {
protected:
int x;
};
class B:public A
{
public:
int y;
void sety(int d)
{
y=d;
}
int gety(){ return y;}
};
int main()
{
B obj;
obj.sety(10);
cout<<obj.gety();
getch();
}
How can I set the value of the protected
instance variable A::x
from an instance of the derived class B
without creating an instance of class A
.
如何从派生的protected
实例设置实例变量的值而不创建.A::x
class B
class A
EDIT: Can we access the value of A::x
using the object of B? Like obj.x
?
编辑:我们可以访问A::x
使用 B 对象的值吗?喜欢obj.x
?
采纳答案by ildjarn
B
is anA
, so creating an instance of B
is creating an instance of A
. That being said, I'm not sure what your actual question is, so here's some code that will hopefully clarify things:
B
是A
,所以创建 的实例B
就是创建 的实例A
。话虽如此,我不确定您的实际问题是什么,所以这里有一些代码有望澄清问题:
class A
{
protected:
int x;
};
class B : public A
{
public:
int y;
int gety() const { return y; }
void sety(int d) { y = d; }
int getx() const { return x; }
void setx(int d) { x = d; }
};
int main()
{
B obj;
// compiles cleanly because B::sety/gety are public
obj.sety(10);
std::cout << obj.gety() << '\n';
// compiles cleanly because B::setx/getx are public, even though
// they touch A::x which is protected
obj.setx(42);
std::cout << obj.getx() << '\n';
// compiles cleanly because B::y is public
obj.y = 20;
std::cout << obj.y << '\n';
// compilation errors because A::x is protected
obj.x = 84;
std::cout << obj.x << '\n';
}
obj
can access A::x
just as an instance of A
could, because obj
is implicitly an instance of A
.
obj
可以A::x
像can的实例一样访问A
,因为obj
它隐式是 的实例A
。
回答by Tilman Vogel
A::x
is protected, so not accessible from outside, neither as A().x
or B().x
. It is however accessible in methods of A
and those directly inheriting it (because protected, not private), e.g. B
. So, regardless of semantics B::sety()
may access it (as plain x
or as A::x
in case of shadowing by a B::x
or for pure verbosity).
A::x
受保护,因此无法从外部访问,无论是 asA().x
还是B().x
。然而,它可以在方法A
和直接继承它的方法中访问(因为受保护,而不是私有),例如B
. 因此,无论语义如何都B::sety()
可以访问它(作为简单的x
或A::x
在被 aB::x
或纯粹冗长的阴影的情况下)。
回答by CashCow
Note that B does not have FULL access to A::x. It can only access that member through an instance of a B, not anything of type A or deriving from A.
请注意,B 没有对 A::x 的完全访问权限。它只能通过 B 的实例访问该成员,而不能通过 A 类型或从 A 派生的任何内容访问该成员。
There is a workaround you can put in:
您可以输入一个解决方法:
class A
{
protected:
int x;
static int& getX( A& a )
{
return a.x;
}
static int getX( A const& a )
{
return a.x;
}
};
and now using getX, a class derived from A (like B) can get to the x member of ANY A-class.
现在使用 getX,从 A 派生的类(如 B)可以访问任何 A 类的 x 成员。
You also know that friendship is not transitive or inherited. The same "workaround" can be made for these situations by providing access functions.
你也知道友谊不是传递的或继承的。通过提供访问功能,可以针对这些情况制定相同的“解决方法”。
And in your case you can actually provide "public" access to the x through your B by having public functions that get to it. Of course in real programming it's protected for a reason and you don't want to give everything full access, but you can.
在您的情况下,您实际上可以通过使用公共函数来通过 B 提供对 x 的“公共”访问。当然,在实际编程中,它受到保护是有原因的,您不想让所有内容都完全访问,但您可以。
回答by Alex Deem
You can just refer to it simply as x
in class B
您可以像x
在 B 类中一样简单地引用它
For example:
例如:
class B : public A
{
public:
...
void setx(int d)
{
x=d;
}
};