473,748 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Possible to avoid RTTI?


Given this situation:

class Base
{
public:
virtual ~Base();

virtual bool equals(Base const&) const = 0;
};

Base::~Base() {}

inline bool operator==(cons t Base& a, const Base& b)
{
return a.equals(b);
}

class Derived : public Base
{
public:
bool equals(const Base& b) const
{
if (typeid(*this) == typeid(b))
{
return data_ == b.data_;
}
return false;
}
private:
int data_;
};

// in use
Base& a(getA());
Base& b(getB());
return a == b;

Is there any way I can do this without RTTI?

I was thinking of something like:

class Base
{
public:
virtual ~Base();

virtual bool equals(Base const&) const = 0;

protected:
virtual void* type_() const = 0;
};

inline bool operator==(cons t Base& a, const Base& b)
{
return a.equals(b);
}

class Derived : public Base
{
public:
bool equals(Base const&) const;

protected:
void* type_() const { return &i; }

private:
static int const i;
};

int const Derived::i = 0;

But are there any other ways to do this?

indi

Jul 22 '05 #1
2 1728
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote in message
news:ML******** ************@ro gers.com...
bool equals(const Base& b) const
{
if (typeid(*this) == typeid(b))
{
return data_ == b.data_;
}
return false;
}

But are there any other ways to do this?


This is a classic multimethod dispatch problem, which doesn't have clear
solutions in C++.
Introductory material may be found here
http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html
There are other ways to implement this, e.g. see Alexandrescu.

You may try to constraint the set of possible overrides to see if a simple
double dispatch or something like that fits.

Hope it helps,
Ruslan Abdikeev.
Jul 22 '05 #2
Mark A. Gibbs wrote:

Is there any way I can do this without RTTI?


Yes, there are certainly more ways. One is:

make an enum like

enum EClassTypes {baseClass, derivedClass};

and implement

EClassType Base::Type()
{
return baseClass;
}

EClassType Derived::Type()
{
return derivedClass;
}

In the comparison you have to replace
*if*(typeid(*th is)*==*typeid(b ))
by
if (Type() == b.Type())
Greetings
Ernst
--
Ernst Murnleitner
www.awite.com
Jul 22 '05 #3

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

Similar topics

2
5978
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
1946
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return #name; } #define DEF_RTTI(name) inline const char *Name(){ return #name; } And(for example) class CProva
9
4628
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This doesn't sound too bad. What I'm worried about is whether there is a general performance overhead caused by enabling RTTI, such as enabling exceptions makes a C++ program slower even when there are no exceptions actually used. Are there similar...
2
3779
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram needs of my dll? Does it bloat the actual download size - would my dll be smaller without it? thanks - I'm using rtti in some instances, but I jsut want to know if it's costing me hundreds of K in extra download. -denny-
3
1620
by: Steven T. Hatton | last post by:
I'm trying to work out a design for dynamically determining file types, and for generating new files of a given type. I'm curious to know what others think of my current strategy. Is it "so einfach wie möglich machen, aber nicht einfacher" or "Rube Goldberg"? The first part of this has to do with a technique used in the C++ Standard Library, so I suspect the purist will not have any objection. It's the approach used to build the...
10
4553
by: Neo | last post by:
what is RTTI? and how to implements it? give me sourceful link for learn and implements it (using standard C mean portable). regards, -aims
3
491
by: Neo | last post by:
Hi Friends, I have a question about RTTI. 1) In C++ we have vptr pointing to vtables which helps to make use of pointer as polymorphic entities. 2) Without knowing object type I can call methods of object using those pointers. So in which case RTTI is needed? ( Why I need to know my object type?) Regards,
5
3023
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined data types, so I have to implement the RTTI by myself. I need a simple and effective example to help me decide how to design. Can someon help me?
2
2247
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks integers, then this procedure will be more fast, so I create something like this: --------------------------------------------- class A { public:
0
8995
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
8832
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
9381
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
9254
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
6078
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();...
0
4608
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
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.