473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimal Efficiency: dynamic_cast pointer or reference?


dynamic_cast can be used to obtain a pointer or to obtain a reference.

If the pointer form fails, then you're left with a null pointer.

If the reference form fails, then an exception is thrown.

Would "Feed1" or "Feed2" be preferable in the following:

#include <iostream>

#include <typeinfo>

using std::bad_cast;

using std::cout;
using std::endl;

class Mammal
{
public:

Mammal()
{
cout << "Constructo r: Mammal" << endl;
}

virtual ~Mammal()
{
cout << "Destructor : Mammal" << endl;
}

/*************** *************** ******
Virtual destructor because Mammal
has to be polymorphic.
*************** *************** ******/
};
class Panda : public Mammal {
public:

Panda()
{
cout << "Constructo r: Panda" << endl;
}

~Panda()
{
cout << "Destructor : Panda" << endl;
}
};
void TellZooKeeperTh atWeFedAPanda()
{
cout << "Hey Zoo Keeper, we just fed a Panda." << endl;
}

void Feed1(Mammal &mammal)
{
if ( dynamic_cast< Panda* > ( &mammal ) )
{
TellZooKeeperTh atWeFedAPanda() ;
}
}

void Feed2(Mammal &mammal)
{
try
{
dynamic_cast< Panda& > (mammal);

TellZooKeeperTh atWeFedAPanda() ;
}
catch ( bad_cast ) { }
}

int main()
{
Mammal panda;

Feed1( panda );

Feed2( panda );
}

-Tomás
Feb 24 '06 #1
1 2271
Tomás wrote:
[...efficiency difference question...]


Let me ask a leading question in response: what efficiency difference
_do_ you _notice_ or _encounter_ in your application/system that prompts
you to ask this efficiency question? Hint: if you can't see any, there
is none.

V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #2

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

Similar topics

2
1858
by: exits funnel | last post by:
Hello, I have the following simple code //BEGIN CODE #include <iostream> class foo { public: virtual void fun( ) { }}; class bar : public foo {public:void fun( ) { }}; int main( )
8
2583
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As it is the case in the following example: ----------------------------------------------------------- #include <iostream>
3
2527
by: Ganesh | last post by:
On devx site, I saw following code. It says when a derived class is tried to cast to base type, it looks at the missing vtable and complains if the object is already deleted. I am of the opinion that this doesnt work if the destructor is not virtual or when the class has no virtual members. I would like to know is there anything wrong in what...
5
2028
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler). You can find a problem there, which cannot be solved by dynamic_cast, because the pointers can be NULL, and I only want to know, if the pointer type...
22
4789
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look now at this code: Base *b = new Level2(); Level1 *l1 = dynamic_cast<Level1*>(b); Should dynamic_cast return a valid pointer or 0? I wonder as...
4
6676
by: yinglcs | last post by:
Hi, I have a c++ application which crashes in this line (from the debugger, I have a segmentation fault here): void *object = dynamic_cast<void>(aObject); I have stepped thru the code in debugger and the 'aObject' pointer is Not null. So can you please tell me, what else can go wrong so that my program crashes in this line?
8
5423
by: pietromas | last post by:
In the example below, why does the dynamic_cast fail (return NULL)? It should be able to cast between sibling classes ... #include <iostream> class A { public: virtual const int get() const = 0;
13
2109
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
25
3113
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or ``GrandChild'', i.e. types not upper than Child in the above class hierarchy, dynamic_cast<Child*>pBase
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
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...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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...
1
5367
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
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...
1
1932
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
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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...

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.