Why is static_cast from pointer to base to pointer to "invalid"?

So, I have this code:

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

mTree-> getNode (groupID) returns PCSNode *. Node is publicly derived from PCSNode.

All the documents that I found in static_cast say this: "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."

However, the Xcode compiler (GCC) says that static_cast from PCSNode * to Node * is invalid and not allowed.

Any reason why this is so? When I switch it to C-style, the compiler has no complaints.

Thank.

UPDATE: Despite the fact that the question has been answered, I will post a compiler error for completeness if anyone else has a problem:

error: Semantic Issue: Static_cast 'PCSNode *' 'Node *'

+5
1

, , Node (, : class Node;).

:

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
}
+22

All Articles