473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Polymorphic types without virtual functions

In lots of places in a programm I need to identify type of received
messages, so I create them as virtual classes and use RTTI to find their
type later. But these are simple messages, often without content, and I
hate how I make their base class: by adding a dummy virtual function.

Is there another way?

--

Regards,
Karel Miklav
Sep 3 '05 #1
9 2095

"Karel Miklav" <ka***@lovetemple.adbloccker.net> wrote in message
news:df*********@enews2.newsguy.com...
In lots of places in a programm I need to identify type of received
messages, so I create them as virtual classes and use RTTI to find their
type later. But these are simple messages, often without content, and I
hate how I make their base class: by adding a dummy virtual function.

Is there another way?

--

Regards,
Karel Miklav


I'm sure there are better ways other than relying on RTTI. However, before
we know what a "message" means in your context and how it is supposed to be
used there's not much we can help.

Perhaps explain to us with more detail? Better still, post some code.

Ben
Sep 3 '05 #2
Karel Miklav wrote:
In lots of places in a programm I need to identify type of received
messages, so I create them as virtual classes and use RTTI to find their
type later. But these are simple messages, often without content, and I
hate how I make their base class: by adding a dummy virtual function.

Is there another way?


There no way to make a class ploymorphic without using a virtual
function. Normally the destructor is used for this purpose, not a dummy
function.

class polymorphic_case
{
public:
virtual ~polymorphic_case() {}
};
Is there are better way than using RTTI? Well that depends.

john
Sep 3 '05 #3
Karel Miklav <ka***@lovetemple.adbloccker.net> wrote in
news:df*********@enews2.newsguy.com:
In lots of places in a programm I need to identify type of received
messages, so I create them as virtual classes and use RTTI to find their
type later. But these are simple messages, often without content, and I
hate how I make their base class: by adding a dummy virtual function.

Is there another way?


Member variable enum determining which type they are?
Sep 3 '05 #4
John Harrison wrote:
There no way to make a class ploymorphic without using a virtual
function. Normally the destructor is used for this purpose, not a dummy
function.

class polymorphic_case
{
public:
virtual ~polymorphic_case() {}
};
It'll have to do. Thanks.
Is there are better way than using RTTI? Well that depends.


Shure, it's just the path of least resistance :)

--

Regards,
Karel Miklav
Sep 3 '05 #5
On Sat, 03 Sep 2005 06:49:48 +0200, Karel Miklav
<ka***@lovetemple.adbloccker.net> wrote:
In lots of places in a programm I need to identify type of received
messages, so I create them as virtual classes and use RTTI to find their
type later. But these are simple messages, often without content, and I
hate how I make their base class: by adding a dummy virtual function.

Is there another way?


Using RTTI to implement polymorphic behavior is usually an indication of a
design problem. Unless you're serializing/unserializing objects, RTTI is
seldom necessary.

Perhaps you can explain more about the problem you are trying to solve?
Sep 3 '05 #6
Dave Rahardja wrote:
Using RTTI to implement polymorphic behavior is usually an indication
of a design problem. Unless you're serializing/unserializing objects,
RTTI is seldom necessary.

Perhaps you can explain more about the problem you are trying to
solve?


I have a kind of ... game. This game gets messages about UI events
from a platform dependant layer. Messages are various but simple, like:
class some_display_event
{
public:

virtual ~some_display_event() { };
};

class reconfigure_event : public some_display_event
{
public:

int width;
int height;

reconfigure_event(int _width, int _height) :
width(_width), height (_height) { };

private:

reconfigure_event();
};

class quit_event : public some_display_event { };

....
Game knows how to use the UI, but the UI knows nothing about the game;
of course they both speak the same message-passing language. The game
then polls the UI from time to time:
bool loop_endlessly = true;
while(loop_endlessly)
{
...

some_display_event * event = display.check_events();
if(event)
{
if(reconfigure_event * re =
dynamic_cast<reconfigure_event *>(event))
{
reconfigure(re->width, re->height);
delete re;
}
else if(quit_event * qe = dynamic_cast<quit_event *>(event))
{
delete qe;
loop_endlessly = false;
}
else
{
delete event;
throw 38122178;
}
}

...
}
Now this game can also draw nice things, consisting of nice and simple
drawing primitives. These primitives are stored in containers, so they
must be ... polymorphic. And here we go again!

What do you say?

--

Regards,
Karel Miklav
Sep 3 '05 #7
On Sat, 03 Sep 2005 23:57:14 +0200, Karel Miklav
<ka***@lovetemple.adbloccker.net> wrote:
I have a kind of ... game. This game gets messages about UI events
from a platform dependant layer. Messages are various but simple, like:
class some_display_event
{
public:

virtual ~some_display_event() { };
};

class reconfigure_event : public some_display_event
{
public:

int width;
int height;

reconfigure_event(int _width, int _height) :
width(_width), height (_height) { };

private:

reconfigure_event();
};

class quit_event : public some_display_event { };

...


Ah, the classical polymorphic event queue problem! The use of RTTI in this
case is not only messy (as you have discovered), it is also _very_ expensive
in the run-time domain, as each event will take on average N/2 dynamic_cast's
to determine what event it actually is (where N is the total number of event
classes). If you receive several thousand messages per second (typical for a
GUI), the lookup overhead will be significant.

I suspect what you want is an event _interface_:

class some_display_event
{
public:
virtual void do() = 0; // pure virtual
};

and each event will implement do() in its own intelligent way:

class reconfigure_event: public some_display_event
{
public:
int width;
int height;

reconfigure_event(int width, int height);
virtual void do();
};

void reconfigure_event::do()
{
renconfigure(width, height);
}

and your event loop will look like:

while(loop_endlessly)
{
/* ... */

some_display_event* event = display.check_events();
event->do();

/* ... */
}

Now the overhead for processing each event is constant, i.e. one virtual
function address lookup.

-dr
Sep 5 '05 #8
Dave Rahardja wrote:
Ah, the classical polymorphic event queue problem! The use of RTTI in
this case is not only messy (as you have discovered), it is also
_very_ expensive in the run-time domain, as each event will take on
average N/2 dynamic_cast's to determine what event it actually is
(where N is the total number of event classes). If you receive
several thousand messages per second (typical for a GUI), the lookup
overhead will be significant.

I suspect what you want is an event _interface_:

class some_display_event
{
public:
virtual void do() = 0; // pure virtual
};


Dave, thanks very much. In the mean time I've come to the same solution
for my other problem (graphic primitive list) and you won't believe
this - my interface has the same do() function :)

I have afterthoughts in this case as I don't want the user interface to
know how events are implemented but it might work if I keep the header
clean.

--

Regards,
Karel Miklav
Sep 6 '05 #9
Karel Miklav wrote:

I have afterthoughts in this case as I don't want the user interface to
know how events are implemented but it might work if I keep the header
clean.


Look into the Pimpl idiom as well. This is pretty much a wrapper around
a pointer, whose implementation details you want hidden.
Sep 6 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Theese | last post by:
Please consider this code: class base {}; class derived: public base {}; base *ptr = new derived; cout << typeid(*base).name << endl; In this case, I see output of "class base" rather than...
2
by: Aryeh M. Friedman | last post by:
If I have something like this: class NumberException { }; class Number { public: ... virtual unsigned long getValue() {throw(NumberException);}; ...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
1
by: verec | last post by:
Last week I asked here how I could detect that a T was polymorphic, and received very thoughtful and useful replies that I used straight away. Thanks to all who answered. This week, it turns...
5
by: FefeOxy | last post by:
Hi, > I'm having a debug assertion error within the file dbgdel.cpp with the expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) I traced the origin of the error and it happened as I tried to...
5
by: Ben Pope | last post by:
Hi all, This is not something I've played around with much, but I'm designing some factories and I want a function like this: template<class T> T* Creator() { return new T; }
3
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
11
by: Angus | last post by:
I am developing a server which receives a range of different messages. There are about 12 different message types so I thought that a good idea would be to devise a class for each message type....
7
by: Arindam | last post by:
#include <cstdio> struct Test { void bar() { foo(); } private: virtual void foo() { printf("Test\n"); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.