[英]C++ Access derived class member from base class pointer
If I allocate an object of a class Derived
(with a base class of Base
), and store a pointer to that object in a variable that points to the base class, how can I access the members of the Derived
class?如果我分配类
Derived
的对象(基类为Base
),并将指向该对象的指针存储在指向基类的变量中,我如何访问Derived
类的成员?
Here's an example:下面是一个例子:
class Base
{
public:
int base_int;
};
class Derived : public Base
{
public:
int derived_int;
};
Base* basepointer = new Derived();
basepointer-> //Access derived_int here, is it possible? If so, then how?
No, you cannot access derived_int
because derived_int
is part of Derived
, while basepointer
is a pointer to Base
.不,您不能访问
derived_int
因为derived_int
是Derived
一部分,而basepointer
是指向Base
的指针。
You can do it the other way round though:你可以反过来做:
Derived* derivedpointer = new Derived;
derivedpointer->base_int; // You can access this just fine
Derived classes inherit the members of the base class, not the other way around.派生类继承基类的成员,而不是相反。
However, if your basepointer
was pointing to an instance of Derived
then you could access it through a cast:但是,如果您的
basepointer
指向Derived
一个实例,那么您可以通过basepointer
访问它:
Base* basepointer = new Derived;
static_cast<Derived*>(basepointer)->derived_int; // Can now access, because we have a derived pointer
Note that you'll need to change your inheritance to public
first:请注意,您需要先将继承更改为
public
:
class Derived : public Base
You're dancing on a minefield here.你正在这里的雷区跳舞。 The base class can never know that it's actually an instance of the derived.
基类永远无法知道它实际上是派生类的一个实例。 The safest way to do that would be to introduce a virtual function in the base:
最安全的方法是在基类中引入一个虚函数:
class Base
{
protected:
virtual int &GetInt()
{
//Die horribly
}
public:
int base_int;
};
class Derived : Base
{
int &GetInt()
{
return derived_int;
}
public:
int derived_int
};
basepointer->GetInt() = 0;
If basepointer
points as something other that a Derived
, your program will die horribly, which is the intended result.如果
basepointer
指向Derived
其他东西,您的程序将死得很惨,这就是预期的结果。
Alternatively, you can use dynamic_cast<Derived>(basepointer)
.或者,您可以使用
dynamic_cast<Derived>(basepointer)
。 But you need at least one virtual function in the Base
for that, and be prepared to encounter a zero.但是您需要在
Base
中至少有一个虚函数,并准备好遇到零。
The static_cast<>
, like some suggest, is a sure way to shoot yourself in the foot. static_cast<>
,就像一些人建议的那样,是一种肯定的方式来射击自己的脚。 Don't contribute to the vast cache of "unsafety of the C language family" horror stories.不要对“C 语言家族的不安全性”恐怖故事的大量缓存做出贡献。
It is possible by letting the base class know the type of derived class.通过让基类知道派生类的类型是可能的。 This can be done by making the base class a template of derived type.
这可以通过使基类成为派生类型的模板来完成。 This C++ idiom is called curiously recurring template pattern .
这个 C++ 习惯用法称为奇怪的重复模板模式。
Knowing the derived class, the base class pointer can be static-casted to a pointer to derived type.知道派生类,基类指针可以静态转换为指向派生类型的指针。
template<typename DerivedT>
class Base
{
public:
int accessDerivedField()
{
auto derived = static_cast<DerivedT*>(this);
return derived->field;
}
};
class Derived : public Base<Derived>
{
public:
int field;
};
int main()
{
auto obj = new Derived;
obj->accessDerivedField();
}
//if you know what derived class you are going to use //如果你知道你将使用哪个派生类
Derived* derivedpointer = dynamic_cast < Derived * > basepointer;派生* 派生指针 = dynamic_cast < 派生 * > 基指针;
//then you can access derived class using derivedpointer //然后你可以使用派生指针访问派生类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.