Skip to main content
gamedev.net
🔒 Locked

Inheritance queries

Started by Striken 3 replies 750+ views
Original Post
Striken
Striken
Hi all, just a quick question to wonder if I'm doing this correctly? I have a TerrainQuad class, which is derived from a QuadTreeNode class. The TerrainQuad basically is a quad tree node that includes geometric details, such as vertex positions, normals etc. Now Im trying to call the QuadTree Traverse() function, that accepts QuadTreeNode*s as parameters. However, when I pass in a CTerrainQuad into it, it doesn't in turn use the appropriate CTerrainQuad methods that are used in the traversal, instead uses the CQuadTreeNode's method; so I am having to write a new method in the CTerrainQuad and make the parent virtual so that it does that. Is that right?

//,,,,,,,,,,,,,,,,,,,,,,,,
//various header files...
class CQTree{
public:
    void Traverse(CQTreeNode* node, ...) { 
        if(haschildren) {
            Traverse(node->child(x), ...);
            ...
        }
        if(condition)
            GenChildren(node);     //this will call CQTreeNode::GenChildren() not CTerrainQuad::GenChildren()
    }
}

class CQTreeNode{
public:
    virtual void GenChildren(CQTreeNode* parent);
}

class CTerrain:public CQTree{
public:
}

class CTerrainQuad:public CQTreeNode{
public:
     virtual void GenChildren(CTerrainQuad* parent); //??
}


I mean I can avoid this problem by, as I said, writing new methods, and basically casting everything to a CTerrainQuad... which I get the impression is very wrong? also! Is this a bad thing to do? (same situation)

//in CTerrainQuad - derived CQuadTreeNode:
//using CQuadTreeNode::Parent()
somevert.x = ((CTerrainQuad*)Parent())->Vertex(0,0);
somevert.x = ((CTerrainQuad*)Parent())->Vertex(0,1);


Any rough pointers in the right direction, or useful links on this topic (I'm aware of the basics of inheritance) would be much appreciated. Thanks in advance, Nathan -- edit made class names consistent -- [Edited by - Striken on November 3, 2004 11:30:41 AM]
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
Fruny
Fruny
* What is CTerrainSquare ?
* Use dynamic_cast for polymorphic downcasts when you aren't sure of the actual type of the object.
* Use static_cast in (most) other cases.
* Don't use C-style casts. They're bad for your health.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Striken
Striken
apologies, CTerrainQuad

I started using CTerrainSquare in my actual code, flipped between and now half confused myeslf.

So basically, casting is the way to do it... but proper dynamic casts not c-style casts?
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
Fruny
Fruny
void CTerrainQuad::GenChildren(CTerrainQuad* parent)

does not override

void CQTreeNode::GenChildren(CQTreeNode* parent);

It merely overloads it. You would need

void CTerrainQuad::GenChildren(CQTreeNode* parent)

--

dynamic_cast is a safe downcast - it returns a NULL pointer if the object being so cast isn't really of the destination type. That is:
CTreeNode* a = new CTreeNode;CTreeNode* b = new CTerrainQuad;CTerrainQuad* pa = dynamic_cast<CTerrainQuad*>(a); // NULLCTerrainQuad* pb = dynamic_cast<CTerrainQuad*>(b); // non NULL
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Striken
Striken
aha! Brilliant, Thanks. Such an embarassingly silly (newbie!) mistake to make, It wasn't calling the right GenerateChildren function because i was passing a different param, as you stated.

thanks again:)

-- it means a lot of my nasty static casts can disappear so it cleans things up a lot.
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.