473,856 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2135

"Karel Miklav" <ka***@lovetemp le.adbloccker.n et> wrote in message
news:df******** *@enews2.newsgu y.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_cas e
{
public:
virtual ~polymorphic_ca se() {}
};
Is there are better way than using RTTI? Well that depends.

john
Sep 3 '05 #3
Karel Miklav <ka***@lovetemp le.adbloccker.n et> wrote in
news:df******** *@enews2.newsgu y.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_cas e
{
public:
virtual ~polymorphic_ca se() {}
};
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***@lovetemp le.adbloccker.n et> 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_ev ent
{
public:

virtual ~some_display_e vent() { };
};

class reconfigure_eve nt : public some_display_ev ent
{
public:

int width;
int height;

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

private:

reconfigure_eve nt();
};

class quit_event : public some_display_ev ent { };

....
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_endl essly)
{
...

some_display_ev ent * event = display.check_e vents();
if(event)
{
if(reconfigure_ event * re =
dynamic_cast<re configure_event *>(event))
{
reconfigure(re->width, re->height);
delete re;
}
else if(quit_event * qe = dynamic_cast<qu it_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***@lovetemp le.adbloccker.n et> 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_ev ent
{
public:

virtual ~some_display_e vent() { };
};

class reconfigure_eve nt : public some_display_ev ent
{
public:

int width;
int height;

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

private:

reconfigure_eve nt();
};

class quit_event : public some_display_ev ent { };

...


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_ev ent
{
public:
virtual void do() = 0; // pure virtual
};

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

class reconfigure_eve nt: public some_display_ev ent
{
public:
int width;
int height;

reconfigure_eve nt(int width, int height);
virtual void do();
};

void reconfigure_eve nt::do()
{
renconfigure(wi dth, height);
}

and your event loop will look like:

while(loop_endl essly)
{
/* ... */

some_display_ev ent* event = display.check_e vents();
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_ev ent
{
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
8086
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 "class derived". This is somewhat expected I suppose since my base class does not have any
2
3899
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
2238
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, cast to void *. Specifically, template <typename T> void f(T * t) { void * p = dynamic_cast<void *>(t) ; } will not compile if T isn't of a class that has somewhere at least
1
2266
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 out that detecting whether T is polymorphic clashes with a new requirement, that T be allowed to not be complete. Last week, this code used to compile:
5
2784
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 delete a polymorphic class as follows:
5
3428
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
3917
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* foo; which a complicated code allocates as one of derived's and sets up.
11
1488
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. Then in my base class I for example have a pure virtual function called eg PerformAction. Then in each message class I implement PerformAction. The base class works out what the type of message is and then calls the correct inherited class...
7
1626
by: Arindam | last post by:
#include <cstdio> struct Test { void bar() { foo(); } private: virtual void foo() { printf("Test\n"); }
0
9920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9764
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10699
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10390
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9535
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4580
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 we have to send another system
2
4176
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.