指向基类的指针

发布于 2024-09-30 05:05:38 字数 259 浏览 0 评论 0原文

如果我有以下类:

class A
{
    ...
}

class B
{
    ...
}

class C : public A, public B
{
    ...
}

并且在某处我检测到我拥有的类 B 的指针实际上指向类 C,但函数需要一个指向类 B 的指针code>A,我该怎么做才能获得指向类 A 的指针?

If I have the following classes:

class A
{
    ...
}

class B
{
    ...
}

class C : public A, public B
{
    ...
}

and somewhere I detect that the pointer of class B that I have actually points to a class C, but a function requires a pointer to class A, what can I do to get that pointer to class A?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合约呢 2024-10-07 05:05:38

如果您确定有一个指向 C 对象的 B* 对象,则可以使用一对 static_cast

B* bp = new C();
C* cp = static_cast<C*>(bp);
A* ap = static_cast<A*>(cp);

:跨继承层次结构进行强制转换的方法是使用dynamic_cast,它要求类型是多态的(也就是说,您的类必须至少有一个虚拟成员函数;因为您的基类析构函数应该是虚拟这通常不是问题):

B* bp = new C();
A* ap = dynamic_cast<A*>(bp);

Dynamic_cast 还有一个额外的好处,即如果失败(即,如果 bp 实际上没有指向 C),它会返回 NULL。它的缺点是性能成本较低(static_cast 在运行时实际上是免费的)。

If you know for certain that you have a B* that points to a C object, you can use a pair of static_casts:

B* bp = new C();
C* cp = static_cast<C*>(bp);
A* ap = static_cast<A*>(cp);

The only way to cast across the inheritance hierarchy is to use dynamic_cast, which requires that the type is polymorphic (that is, your class must have at least one virtual member function; since your base class destructors should be virtual, this usually isn't a problem):

B* bp = new C();
A* ap = dynamic_cast<A*>(bp);

dynamic_cast has the added benefit that if it fails (that is, if bp doesn't actually point to a C), it returns NULL. It has the disadvantage of a slight performance cost (static_cast is effectively free at runtime).

稳稳的幸福 2024-10-07 05:05:38

该代码

class A
{
};
class B
{
};
class C : public A, public B
{
};
int main() {
  C c;
  A *a = &c;
}

有效,因为 C 已经是 A,因此分配有效。

The code

class A
{
};
class B
{
};
class C : public A, public B
{
};
int main() {
  C c;
  A *a = &c;
}

is valid since C is already an A, so the assignment is valid.

泛泛之交 2024-10-07 05:05:38

如果 C 继承自 A,如您所示,则 C* 指针应该可以隐式转换为 A* 指针。是否有可能你没有包含类C的声明,所以编译器不知道这种继承关系?或者实际上存在与您的问题中给出的不同的继承关系?一些代码将有助于诊断这个问题。

编辑
根据您问题的更新版本:

// Converts b to type A*, but only if it is actually
// of type C; otherwise, returns NULL
A* convertBtoAviaC(B* b) {
   C* c = dynamic_cast<C*>(b);
   return c; // note may be NULL, if b is not a C
}

If C inherits from A as you have shown, then a C* pointer should be implicitly convertible to an A* pointer. Is it possible that you haven't included the declaration of class C, so that the compiler isn't aware of this inheritance relationship? Or that there is actually a different inheritance relationship than that given in your question? Some code would be helpful in diagnosing this problem.

Edit
Based on the updated version of your question:

// Converts b to type A*, but only if it is actually
// of type C; otherwise, returns NULL
A* convertBtoAviaC(B* b) {
   C* c = dynamic_cast<C*>(b);
   return c; // note may be NULL, if b is not a C
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文