Base or deruved object?

S

Simon Elliott

If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?

The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?

Alternatively I could have an instance variable in the base class which
derived classes are required to set, or a virtual function which
derived classes are required to override. This puts a burden on
developers of derived classes.

Any other options? Which is best?
 
V

Victor Bazarov

Simon said:
If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?

The best way is not to do it.
The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?

There is some. Whenever you're concerned with performance, you need to
test it. Nobody will tell you what it's going to be except a profiler.
Alternatively I could have an instance variable in the base class which
derived classes are required to set, or a virtual function which
derived classes are required to override. This puts a burden on
developers of derived classes.

Any other options? Which is best?

The best is to design your program so that you don't need to try to know
what derived class object it is.

V
 
R

Richard Herring

Simon Elliott said:
If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?

The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?

Possibly. (AIUI at least one compiler out there does something
equivalent to a string comparison.) But it's a clear statement of your
true intent, so I'd use that unless and until profiling indicates that
you really have a performance problem.
Alternatively I could have an instance variable in the base class which
derived classes are required to set,

Ugh. Just Say No to metadata.
or a virtual function which
derived classes are required to override. This puts a burden on
developers of derived classes.

Any other options?

You could provide the override once and for all in a derived class of
your own, and require developers to inherit from that instead of the
real base.
 
R

Ron Natalie

Simon said:
If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?

The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?
You can also use typeid.

The real question is why do you need to know. It's usually the sign
of a bad design for something to have to ask what the type of something
is. Usually, anything that acts "differently" for different types
is implemented in virutal functions in the types.
 
K

Karl Heinz Buchegger

Simon said:
If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?

The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?

Alternatively I could have an instance variable in the base class which
derived classes are required to set, or a virtual function which
derived classes are required to override. This puts a burden on
developers of derived classes.

Any other options? Which is best?

Well

If a question like that arises, then the first thing we need
to ask is: Why do you want to do this?
The reason for this quesiton is, that almost always you are attempting
the wrong thing:
Instead of figuring out which class it is exactly and depending on the
exact type, you should instroduce a virtual function and just call that
instead.

Having said that: dynamic_cast is a possible solution. Certainly better
then having an instance variable. Why? You already figured it out: Everything
the compiler can do for you automatically is always better then your own
invention.
 
M

Mike Wahler

Simon Elliott said:
If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?

The whole point of polymorphism is to obviate such a need.
If you see this need, I recommend that you reconsider your
design.
The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?

Usually yes.
Alternatively I could have an instance variable in the base class which
derived classes are required to set, or a virtual function which
derived classes are required to override. This puts a burden on
developers of derived classes.

Any other options? Which is best?

Take a few steps back and look at the goals and design
of your application. (State them, and perhaps you'll receive
some ideas).

-Mike
 
M

Michael

Ok Guys,
on the same thread
I'm writing a Game engine, which performs collision detection.
I want to sepate the collision boundings libaray from the main game engine
and load it as a library.
If simplified this example but it illustrates the point.... :)

So i declare an abstract base class ( & function ):

///////////////////////////////////////////////////
Bounding & Collision Libaray

//Boundings.h ( only file #include'd by the engine)
///////////////////////
class BoundingObject
{
public:
virtual int GetShape() =0;
};

BoundingObject* CreateBoundingObject( vector<Triangles>& tris);
bool GetCollision(BoundingObject* b1,BoundingObject* b2);

//Boundings.cpp
////////////////////////////////
#define B_SPHERE 1
#define B_BOX 2
#define B_TRIANGLE 3


class BoundingSphere : public BoundingObject
{
// specific data
virtual int GetShape() { return B_SPHERE; }
};

class BoundingBox : public BoundingObject
{
// specific data
virtual int GetShape() { return B_BOX; }
};

class BoundingTriangle : public BoundingObject
{
// specific data
virtual int GetShape() { return B_TRIANGLE; }
};

bool IsCollisionSphereSphere(BoundingSphere* s1,BoundingSphere* s2);
bool IsCollisionSphereBox(BoundingSphere* s1,BoundingBox* s2);
bool IsCollisionSphereTriangle(BoundingSphere* s1,BoundingTriangle* s2);


bool GetCollision(BoundingObject* b1,BoundingObject* b2)
{
if( b1.GetShape() == B_SPHERE)
{
if( b1.GetShape() == B_SPHERE) return IsCollisionSphereSphere(
static_cast<BoundingSphere*> b1,

static_cast<BoundingSphere*> b2 );

if( b2.GetShape() == B_BOX ) return
IsCollisionSphereBox( static_cast<BoundingSphere*> b1,

static_cast<BoundingBox*> b2 );
//etc....
}
//etc

}

//End Bounding & Collision Library
/////////////////////////////////////////////////////////



Then in my game engine:

class object
{
public:
BoundingObject* pBoundingObject;
vector<Triangle> triData;
};



in the game.....


object* pObj1 = new Obj;
// Some specific code.... //
pObj1->pBoundingObject = BoundingObject* CreateBoundingObject(
pObj->triData ) ;

object* pObj2 = new Obj;
// Some specific code.... //
pObj2->pBoundingObject = BoundingObject* CreateBoundingObject(
pObj->triData ) ;

bool IsCol = IsCollision(pObj1,pObj2);


the idea is that the engine need not know how the objects are being bounded,
but the comparisions between each type of object can be optimised in the
bounding library. But it is horribly ugly. I can't even use enums for the
Bounding types as adding different bounding types would mean a recompile for
the engine!
Any thoughts??


Mike
 
M

Michael

Ignore this thread, i started a new one

Michael said:
Ok Guys,
on the same thread
I'm writing a Game engine, which performs collision detection.
I want to sepate the collision boundings libaray from the main game engine
and load it as a library.
If simplified this example but it illustrates the point.... :)

So i declare an abstract base class ( & function ):

///////////////////////////////////////////////////
Bounding & Collision Libaray

//Boundings.h ( only file #include'd by the engine)
///////////////////////
class BoundingObject
{
public:
virtual int GetShape() =0;
};

BoundingObject* CreateBoundingObject( vector<Triangles>& tris);
bool GetCollision(BoundingObject* b1,BoundingObject* b2);

//Boundings.cpp
////////////////////////////////
#define B_SPHERE 1
#define B_BOX 2
#define B_TRIANGLE 3


class BoundingSphere : public BoundingObject
{
// specific data
virtual int GetShape() { return B_SPHERE; }
};

class BoundingBox : public BoundingObject
{
// specific data
virtual int GetShape() { return B_BOX; }
};

class BoundingTriangle : public BoundingObject
{
// specific data
virtual int GetShape() { return B_TRIANGLE; }
};

bool IsCollisionSphereSphere(BoundingSphere* s1,BoundingSphere* s2);
bool IsCollisionSphereBox(BoundingSphere* s1,BoundingBox* s2);
bool IsCollisionSphereTriangle(BoundingSphere* s1,BoundingTriangle* s2);


bool GetCollision(BoundingObject* b1,BoundingObject* b2)
{
if( b1.GetShape() == B_SPHERE)
{
if( b1.GetShape() == B_SPHERE) return IsCollisionSphereSphere(
static_cast<BoundingSphere*> b1,

static_cast<BoundingSphere*> b2 );

if( b2.GetShape() == B_BOX ) return
IsCollisionSphereBox( static_cast<BoundingSphere*> b1,

static_cast<BoundingBox*> b2 );
//etc....
}
//etc

}

//End Bounding & Collision Library
/////////////////////////////////////////////////////////



Then in my game engine:

class object
{
public:
BoundingObject* pBoundingObject;
vector<Triangle> triData;
};



in the game.....


object* pObj1 = new Obj;
// Some specific code.... //
pObj1->pBoundingObject = BoundingObject* CreateBoundingObject(
pObj->triData ) ;

object* pObj2 = new Obj;
// Some specific code.... //
pObj2->pBoundingObject = BoundingObject* CreateBoundingObject(
pObj->triData ) ;

bool IsCol = IsCollision(pObj1,pObj2);


the idea is that the engine need not know how the objects are being bounded,
but the comparisions between each type of object can be optimised in the
bounding library. But it is horribly ugly. I can't even use enums for the
Bounding types as adding different bounding types would mean a recompile for
the engine!
Any thoughts??


Mike
 
S

Simon Elliott

Take a few steps back and look at the goals and design
of your application. (State them, and perhaps you'll receive
some ideas).

The only point where I might want to do this is when I need to create
and destroy objects. Consider something along these lines (unchecked):

Let's say we have a class fooBase and two classes publicly derived
from fooBase: fooDerived1 and fooDerived2. I have a fooFactory class
which creates the correct foo class depending on some given criteria.


class bar
{
private:
foo* foo_;
public:
bar():foo_(0){}
ChangeFoo(int fooType)
{
delete foo_;
foo_ = fooFactory::Instance().CreateInstance(fooType);
}
DoStuffToFoo(int someArg)
{
foo_->DoStuff(someArg);
}
};

OK so far (so long as the user remembers to call bar::ChangeFoo()
before calling bar::DoStuffToFoo(); in real life we probably wouldn't
want something this unsafe.)

The user calls bar::ChangeFoo() which sets up foo_ to point to either
fooDerived1 or fooDerived2. The user calls bar::DoStuffToFoo() which
calls the virtual function DoStuff() which is routed to fooDerived1 or
fooDerived2 as applicable.

But suppose the construction of a fooDerived1 or fooDerived2 was a
nontrivial exercise, and further suppose that a new fooDerived1 or
fooDerived2 is only required when the type changes (ie a call to
ChangeFoo need not result in a pristine new fooDerived1 or
fooDerived2). And let's say that classes derived from fooBase need lots
of memory, or access a unique resource, so that we can't have more than
one existing at any one time.

In this case we might want to know what type of foo we need, and what
type of foo we've already got, to avoid having to destroy and recreate
a foo unnecessarily.
 
D

David Rubin

Michael said:
Ok Guys,
on the same thread
I'm writing a Game engine, which performs collision detection.
I want to sepate the collision boundings libaray from the main game engine
and load it as a library.
If simplified this example but it illustrates the point.... :)

So i declare an abstract base class ( & function ): [...]
class BoundingObject
{
public:
virtual int GetShape() =0;
};
BoundingObject* CreateBoundingObject( vector<Triangles>& tris);
bool GetCollision(BoundingObject* b1,BoundingObject* b2);
//Boundings.cpp
////////////////////////////////
#define B_SPHERE 1
#define B_BOX 2
#define B_TRIANGLE 3

[snip - derived for sphere, box, and triangle]
bool IsCollisionSphereSphere(BoundingSphere* s1,BoundingSphere* s2);
bool IsCollisionSphereBox(BoundingSphere* s1,BoundingBox* s2);
bool IsCollisionSphereTriangle(BoundingSphere* s1,BoundingTriangle* s2);
bool GetCollision(BoundingObject* b1,BoundingObject* b2)
{
if( b1.GetShape() == B_SPHERE)
{
if( b1.GetShape() == B_SPHERE) return IsCollisionSphereSphere(
static_cast<BoundingSphere*> b1,

static_cast<BoundingSphere*> b2 );

if( b2.GetShape() == B_BOX ) return
IsCollisionSphereBox( static_cast<BoundingSphere*> b1,

static_cast<BoundingBox*> b2 );
//etc....
}
//etc

}

I belive this was covered in a DDJ article in the past year. The
solution used double dispatch. I'm sure you can find it online.
/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top