Determine pointer type

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

I've a some classes which are all inherited from the same
base class class_BASE;


There's another class which holds some information and is
"bound" to one of the inherited classes indicated by the member attibute

class_BASE *mElement;

which is set my the constructor.

In a function of that class I need something like
void foo()
{
if ( mElement is of type class_INHERITED_A )
...
else if ( mElement is of type class_INHERITED_B)
...
}

How can I determine the type of this pointer i.e. how can I figure out
which inherited class is represented by mElement?


Thank you.
Chris
 
J

Jakob Bieling

Christian Christmann said:
Hi,

I've a some classes which are all inherited from the same
base class class_BASE;


There's another class which holds some information and is
"bound" to one of the inherited classes indicated by the member
attibute

class_BASE *mElement;

which is set my the constructor.

In a function of that class I need something like
void foo()
{
if ( mElement is of type class_INHERITED_A )
...
else if ( mElement is of type class_INHERITED_B)
...
}

How can I determine the type of this pointer i.e. how can I figure out
which inherited class is represented by mElement?


If you need that, your design is flawed, IMHO. Why not implement foo
as a member function of your classes and each class implements it by
using whatever "..." is in your snippet above?

regards
 
T

Tobias Blomkvist

Christian said:
Hi,

I've a some classes which are all inherited from the same
base class class_BASE;


There's another class which holds some information and is
"bound" to one of the inherited classes indicated by the member attibute

class_BASE *mElement;

which is set my the constructor.

In a function of that class I need something like
void foo()
{
if ( mElement is of type class_INHERITED_A )
...
else if ( mElement is of type class_INHERITED_B)
...
}

#include <typeinfo>
#include <iostream>

if( typeid( *mElement ) == typeid( class_INHERITED_A ) )
{
std::cout<<"mElement is type class_INHERITED_A"<<std::endl;
}

Tobias
 
M

Matthias Kaeppler

Christian said:
How can I determine the type of this pointer i.e. how can I figure out
which inherited class is represented by mElement?

You're taking the wrong approach. If you have operations which depend of
the type of the object and if this object is used through a base class
pointer, you should utilize polymorphic behavior for these objects.

In other words, you have the type-dependent behavior in the wrong place;
it is supposed to occur in your derived types, not in your info class.
Try something like this:

struct Base {
virtual void dependent_op() = 0;
};

struct A: Base {
virtual void dependent_op() {
/* invoke A-specific behavior */
}
};

struct B: Base {
virtual void dependent_op() {
/* invoke B-specific behavior */
}
};


void InfoClass::type_dependent_op() {
m_element->dependent_op(); // does whatever is correct
// for the object pointed to
// by m_element
}

Regards,
Matthias
 
M

Matthias Kaeppler

In addition to my other post, a simple approach to determining the
dynamic type of an object would be to write a dynamic type function:

enum TypeTag {
TYPE_A,
TYPE_B
};

struct Base {
virtual TypeTag get_type() const = 0;
};

struct A: Base {
virtual TypeTag get_type() const {
return TYPE_A;
}
};

struct B: Base {
virtual TypeTag get_type() const {
return TYPE_B;
}
};

void InfoClass::foo() {
switch (m_element->get_type()) {
case TYPE_A:
// ...
break;
case TYPE_B:
// ...
break;
}
}
 
R

red floyd

Christian said:
Hi,

I've a some classes which are all inherited from the same
base class class_BASE;


There's another class which holds some information and is
"bound" to one of the inherited classes indicated by the member attibute

class_BASE *mElement;

which is set my the constructor.

In a function of that class I need something like
void foo()
{
if ( mElement is of type class_INHERITED_A )
...
else if ( mElement is of type class_INHERITED_B)
...
}

How can I determine the type of this pointer i.e. how can I figure out
which inherited class is represented by mElement?

In general, this is considered bad design -- it's not scalable, etc...
(e.g. what hapens if you define class_INHERITED_C,... class_INHERITED_Z?).

This sort of type specific behavior is perfect for virtual functions.

class_BASE {
public:
virtual void foo_behavior() = 0;
};

class_INHERITED_A {
public:
void foo_behavior() {
// A specific foo behavior here
}
};

class_INHERITED_B {
public:
void foo_behavior() {
// B specific foo behavior here
}
};

void foo()
{
mElement->foo_behavior();
}
 

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,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top