C++从基类指针访问派生类成员
如果我分配一个 Derived
类的对象(基类为 Base
),并将指向该对象的指针存储在指向基类的变量中,如何我可以访问 Derived
类的成员吗?
这是一个例子:
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?
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,您无法访问
衍生_int
,因为衍生_int
是Derived
的一部分,而basepointer
是指向的指针基础
。不过,您也可以反过来做:
派生类继承基类的成员,而不是相反。
但是,如果您的
basepointer
指向Derived
的实例,那么您可以通过强制转换来访问它:请注意,您需要将继承更改为
public首先:
No, you cannot access
derived_int
becausederived_int
is part ofDerived
, whilebasepointer
is a pointer toBase
.You can do it the other way round though:
Derived classes inherit the members of the base class, not the other way around.
However, if your
basepointer
was pointing to an instance ofDerived
then you could access it through a cast:Note that you'll need to change your inheritance to
public
first:你在这里的雷区跳舞。基类永远无法知道它实际上是派生类的实例。最安全的方法是在基类中引入一个虚函数:
如果
basepointer
指向Derived
之外的其他东西,你的程序将会可怕地死掉,这就是预期的结果。或者,您可以使用
dynamic_cast(basepointer)
。但是为此,您在Base
中至少需要一个虚函数,并准备好遇到零。就像一些人建议的那样,
static_cast<>
肯定是搬起石头砸自己的脚。不要为大量“C 语言家族的不安全”恐怖故事做出贡献。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:
If
basepointer
points as something other that aDerived
, your program will die horribly, which is the intended result.Alternatively, you can use
dynamic_cast<Derived>(basepointer)
. But you need at least one virtual function in theBase
for that, and be prepared to encounter a zero.The
static_cast<>
, like some suggest, is a sure way to shoot yourself in the foot. Don't contribute to the vast cache of "unsafety of the C language family" horror stories.您可以使用 CRTP
您基本上在基类的模板中使用派生类
you can use CRTP
you basically use the derived class in the template for the base class
通过让基类知道派生类的类型是可能的。这可以通过使基类成为派生类型的模板来完成。这个 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.
Knowing the derived class, the base class pointer can be static-casted to a pointer to derived type.
//如果你知道你将使用什么派生类
Derived*衍生指针=dynamic_cast<导出*>基指针;
//然后你可以使用派生指针访问派生类
//if you know what derived class you are going to use
Derived* derivedpointer = dynamic_cast < Derived * > basepointer;
//then you can access derived class using derivedpointer