473,320 Members | 1,859 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,320 software developers and data experts.

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 << "Constructor: Mammal" << endl;
}

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

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

Panda()
{
cout << "Constructor: Panda" << endl;
}

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

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

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

TellZooKeeperThatWeFedAPanda();
}
catch ( bad_cast ) { }
}

int main()
{
Mammal panda;

Feed1( panda );

Feed2( panda );
}

-Tomás
Feb 24 '06 #1
1 2259
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
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
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...
3
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...
5
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)....
22
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...
4
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...
8
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()...
13
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.