473,473 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Determine pointer type

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
5 6477
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
16
by: Jm | last post by:
Hi All Is it possible to determine who is logged onto a machine from inside a service using code in vb.net ? I have found some code that seems to work under vb6, but doesnt under .NET ? Any help...
5
by: yancheng.cheok | last post by:
hello, may i know how i can determine whether a pointer is pointing to an array or a single item during runtime? this is because in certain situation, i need to determine whether to use delete...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
13
by: softwaredoug | last post by:
I can't see to easily find this on google or in a newsgroup Is there a standard function/macro/whatever you can call and determine the distance in a C program how deep one is in the C call stack...
6
by: Kai Rosenthal | last post by:
Hello, how can I determine the architecture (32 or 64bit) with python 2.2 on Windows or Unix (AIX, Solaris) OS, without the modul platform? Thanks for your hints, Kai
9
by: paktsardines | last post by:
Dear all, As of yesterday I have this function: char ** lines = read_file("filename.txt"); Now, I want to print the contents of lines. A first attempt: int i=0;
5
by: Paul | last post by:
hi, there, I was asked such a question: how to determine the size of memory of the int pointer pointed? for example int GetTheSizeofMemory(int *buffer) { int size; //enter your code here,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.