简体   繁体   English

C++ 从基类指针访问派生类成员

[英]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_intDerived一部分,而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 语言家族的不安全性”恐怖故事的大量缓存做出贡献。

you can use CRTP你可以使用CRTP

you basically use the derived class in the template for the base class您基本上在基类的模板中使用派生类

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.

相关问题 C ++访问派生类与基类的指针 - c++ access derived class with a pointer from base class C ++将派生类的成员指针转换为基类指针 - c++ cast member pointer of derived Class to base class pointer C ++将私有成员从派生类访问到另一个派生类(两者都具有相同的基类) - C++ Access private member from a derived class to another derived class (both have the same base class) C ++如何在不使用static_cast或dynamic_cast的情况下从基类指针访问派生类成员? - C++ How to access derived class member from base class pointer without using a static_cast or dynamic_cast? 从基类指针访问派生类成员的设计替代方案 - Design alternative for access to derived class member from base class pointer 从基类的指针访问派生类的成员 - Access member of derived class from pointer of base class 基类可以访问 C++ 中的派生类受保护成员吗? - Can a base class access a derived class protected member in c++? C ++通过指向基类的派生类中的指向成员函数调用 - C++ call via pointer-to-member function in a derived class from the base class C ++使用基类指针访问派生类方法 - C++ access a derived class method using a base class pointer 通过基类指针C ++访问派生类的成员 - Access members of derived class through base class pointer C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM