[英]How to access a private member in the baseclass from a subclass with inheritance ( C++ )
I am currently busy with inheritance and I'm working with a base class called Vehicle
and a subclass called Truck
. 我目前正忙于继承,我正在使用一个名为Vehicle
的基类和一个名为Truck
的子类。 The subclass is inheriting from the base class. 子类继承自基类。 I am managing with the inheritance of the public members of Vehicle
but can't access the private member called top_speed
in the function void describe() const
. 我正在管理Vehicle
的公共成员的继承,但无法在函数void describe() const
访问名为top_speed
的私有成员。
I know that one can do this in code (provided below) to access from the base class but it seems like I'm missing something. 我知道可以在代码(下面提供)中执行此操作以从基类访问,但似乎我遗漏了一些东西。 In the comment section in the code my question is stated more clearly for you. 在代码的评论部分,我的问题更明确地为您说明。
void Truck::describe() const : Vehicle()
/*I know this is a way-> : Vehicle()
to inherit from the Vehicle class but how
can I inherit the top_speed private member of Vehicle in the
void describe() const function of the Truck class?*/
{
cout << "This is a Truck with top speed of" << top_speed <<
"and load capacity of " << load_capacity << endl;
}
Where in my Vehicle
class it is: 在我的Vehicle
类中它是:
class Vehicle
{
private:
double top_speed;
public:
//other public members
void describe() const;
};
and in the Truck class it is this: 在卡车类中它是这样的:
class Truck: public Vehicle
{
private:
double load_capacity;
public:
//other public members
void describe() const;
};
To be even more clear I am getting this error: 为了更清楚我收到这个错误:
error: 'double Vehicle::top_speed' is private
What can I do in the void Truck::describe() const
function to fix it? 我可以在void Truck::describe() const
函数中做些什么来修复它?
Simply declare top_speed
as protected
只需将top_speed
声明为protected
class Vehicle
{
protected:
double top_speed;
public:
//other public members
void describe() const;
};
If you can't modify the base class then you just can't access it. 如果您无法修改基类,则无法访问它。
The cleanest way is to have a public accessor function in the base class, returning a copy of the current value. 最干净的方法是在基类中使用公共访问器函数,返回当前值的副本。 The last thing you want to do is expose private data to subclasses(*). 您要做的最后一件事是将私有数据暴露给子类(*)。
double Vehicle::getTopSpeed( void ) const {
return this->top_speed;
}
and then use it in the Truck class like this: 然后在Truck类中使用它,如下所示:
void Truck::describe( void ) const {
cout << "This truck's top speed is " << this->getTopSpeed() << endl;
}
(Yeah, I know, "this->" is superfluous but it's used for illustrating that it's accessing members/methods of the class) (是的,我知道,“this->”是多余的,但它用于说明它正在访问该类的成员/方法)
(*) Because, really, if you're going to expose data members of superclass(es), then why would you use private/protected/public at all? (*)因为,实际上,如果您要公开超类的数据成员,那么为什么要使用private / protected / public?
Joe_B said Joe_B说
I am not allowed to make changes to the Vehicle class given 我不允许对给定的Vehicle类进行更改
Then, there is still a very hugly way to access private members of a class you cannot modify (3rd party for instance). 然后,仍然有一种非常丑陋的方式来访问您无法修改的类的私有成员(例如第三方)。
If verhicle.h (let's say that's the class you have no way to modify) is: 如果verhicle.h(让我们说这是你无法修改的类)是:
class Vehicle
{
private:
double top_speed;
public:
Vehicle( double _top_speed ) : top_speed( _top_speed ) {}
};
and truck.h is: 和truck.h是:
#include "vehicle.h"
class Truck: public Vehicle
{
private:
double load_capacity;
public:
Truck( double _load_capacity, double _top_speed ) :
Vehicle( _top_speed ),
load_capacity( _load_capacity )
{}
void describe() const
{
std::cout << "This is a Truck with top speed of "
<< top_speed
<< " and load capacity of "
<< load_capacity
<< std::endl;
}
};
Compiling this will give the error 编译它会产生错误
Vehicle::top_speed is private Vehicle :: top_speed是私有的
Then, let's do this: 然后,让我们这样做:
In trunk.h, change #include "vehicle.h"
into: 在trunk.h中,将#include "vehicle.h"
vehicle.h #include "vehicle.h"
更改为:
#define private friend class Truck; private
#include "vehicle.h"
#undef private
Without modifying vehicle.h, you made Truck class be a friend of Vehicle class, and it can now access Vehicle's private members! 在不修改vehicle.h的情况下,您使Truck类成为Vehicle类的朋友,它现在可以访问Vehicle的私人成员!
Please, do not comment that it is a very bad idea to do this as it breaks all C++ rules....I know, you only want to do this if: 请不要评论这样做是一个非常糟糕的主意,因为它打破了所有C ++规则....我知道,你只想这样做:
I used to do this when customizing behaviour of some MFC classes in the past...I definitely could not change the header files defining them.... 我曾经在过去定制某些MFC类的行为时这样做...我绝对无法更改定义它们的头文件....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.