简体   繁体   English

为什么从指向 Base 的指针到指向 Derived 的指针的 static_cast “无效”?

[英]Why is a static_cast from a Pointer to Base to a Pointer to Derived “invalid?”

So I have this code:所以我有这个代码:

Node* SceneGraph::getFirstNodeWithGroupID(const int groupID)
{
    return static_cast<Node*>(mTree->getNode(groupID));
}

mTree->getNode(groupID) returns a PCSNode*. mTree->getNode(groupID) 返回一个 PCSNode*。 Node is publicly derived from PCSNode. Node 是从 PCSNode 公开派生的。

All of the docs I've found on static_cast say something to this effect: "The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class."我在 static_cast 上找到的所有文档都说明了这一点:“static_cast 运算符可用于诸如将指向基本 class 的指针转换为指向派生 class 的指针等操作。”

Yet, XCode's (GCC) compiler says that the static_cast is from PCSNode* to Node* is invalid and not allowed.然而,XCode 的 (GCC) 编译器表示从 PCSNode* 到 Node* 的 static_cast 无效且不允许。

Any reason why this is?这是什么原因? When I switch it to a C-style cast, there are no complaints from the compiler.当我将它切换到 C 风格的演员表时,编译器没有抱怨。

Thanks.谢谢。

UPDATE: Even though the question was answered, I'll post the compiler error for completeness in case anyone else has the same problem:更新:即使问题已得到解答,我仍会发布编译器错误以确保完整性,以防其他人遇到同样的问题:

error: Semantic Issue: Static_cast from 'PCSNode *' to 'Node *' is not allowed错误:语义问题:不允许从“PCSNode *”到“Node *”的静态转换

The reason is most likely that the definition of Node is not visible to the compiler (eg, it may have been only forward-declared: class Node; ).原因很可能是Node的定义对编译器不可见(例如,它可能只是前向声明的: class Node; )。

Self-contained example:自包含示例:

class Base {};

class Derived; // forward declaration

Base b;

Derived * foo() {
    return static_cast<Derived*>( &b ); // error: invalid cast
}

class Derived : public Base {}; // full definition

Derived * foo2() {
    return static_cast<Derived*>( &b ); // ok
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从基类指针到派生类指针的static_cast无效 - static_cast from base class pointer to derived class pointer is invalid 什么时候是`static_cast <Base*> (的static_cast <void*> (派生))`从指向派生类的指针有效吗? - When is a `static_cast<Base*>(static_cast<void*>(derived))` from a pointer to a derived class valid? 将派生对象中的“this”指针的static_cast用于基类的问题 - Question of using static_cast on “this” pointer in a derived object to base class 通过指向base,static_cast,crtp,删除模板的指针派生的成员 - Member of derived by pointer to base, static_cast, crtp, removing templates static_cast到base析构函数的指向派生类的安全性 - Safety of static_cast to pointer-to-derived class from base destructor C ++如何在不使用static_cast或dynamic_cast的情况下从基类指针访问派生类成员? - C++ How to access derived class member from base class pointer without using a static_cast or dynamic_cast? static_cast从Derived *到void *到Base * - static_cast from Derived* to void* to Base* 为什么我的指针的 static_cast 失败了? - Why is my static_cast of a pointer failing? 应该static_cast <Derived *> (基本指针)给出编译时错误? - Should static_cast<Derived *>(Base pointer) give compile time error? 使用static_cast &lt;&gt;类型转换指针 - typecasting a pointer with static_cast<>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM