By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

Determine pointer type

P: n/a
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


Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...
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
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #2

P: n/a
Christian Christmann wrote:
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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 23 '05 #3

P: n/a
Christian Christmann wrote:
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

--
Matthias Kaeppler
Jul 23 '05 #4

P: n/a
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;
}
}

--
Matthias Kaeppler
Jul 23 '05 #5

P: n/a
Christian Christmann wrote:
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();
}

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.