What is the meaning of using dynamic_cast without any error handling?
Like

struct base 
{
  virtual void testing() { std::cout<<"base"<<std::endl; }
};

struct derived : public base
{
  virtual void testing() { std::cout<<"derived"<<std::endl; }
};

int main()
{
  base baseInstance;
  derived derivedInstance;
  base *bp = &derivedInstance;
  base &br = derivedInstance;
  
  //cast pointer with error handling
  if(derived *dp = dynamic_cast<derived>(bp)) ...do something with dp

  //cast pointer without error handling
  derived *dp = dynamic_cast<derived>(bp) ...do something with dp
  
  //cast reference with error handling
  try
  {
    derived *dr = dynamic_cast<derived>(bp);
  }
  catch(std::bad_cast &ex) {...do something with dr}

  //cast reference without error handling
  derived *dr = dynamic_cast<derived>(bp);

  return 0;
}

If I don't need to do any error handling, should it be a better idea to use static_cast
rather than dynamic_cast? I think it would be more efficient than dynamic_cast.
(Although the best case is without any pointer or reference casting)

Thank you very much.

Edited 5 Years Ago by stereomatching: n/a

Sorry, my codes had a lot of mistakes
I didn't check it carefully

#include<iostream>
#include<typeinfo>

struct base
{
  virtual void testing() { std::cout<<"base"<<std::endl; }
};

struct derived : public base
{
  virtual void testing() { std::cout<<"derived"<<std::endl; }
};

int main()
{
  base baseInstance;
  derived derivedInstance;
  base *bp = &derivedInstance;
  base &br = derivedInstance;

  //cast pointer with error handling
  if(derived *dp = dynamic_cast<derived*>(bp))
  {
    std:: cout<<"safe"<<std::endl;
  }
  //cast pointer without error handling
  derived *dp = dynamic_cast<derived*>(bp);

  //cast reference with error handling
  try
  {
    derived &dr = dynamic_cast<derived&>(br);
  }
  catch(std::bad_cast &ex)
  {
    std::cout<<ex.what()<<std::endl;
  }

  //cast reference without error handling
  base &br2 = baseInstance;
  derived &dr = dynamic_cast<derived&>(br2);

  return 0;
}

I find out one thing, even I didn't catch the exception of line 41
gcc 4.6.1(minGW) or visual c++ 2010 would throw some error message and
cease the program
Is this the behavior defined by the standard or some compiler extension?

Thank you very much

What is the meaning of using dynamic_cast without any error handling?

It means the author was confident that the cast would succeed. That's somewhat foolish in my opinion. Except for the most basic of code, you can't easily make such a guarantee.

I find out one thing, even I didn't catch the exception of line 41
gcc 4.6.1(minGW) or visual c++ 2010 would throw some error message and
cease the program
Is this the behavior defined by the standard or some compiler extension?

Yes. dynamic_cast using a reference type throws on failure and if you don't catch that exception or set a default handler, std::terminate() is called.

Edited 5 Years Ago by Narue: n/a

std::terminate() is called.

Do you think it would be a good idea to neglect try and catch since
std::terminate would be called even without try and catch?

Besides, in the world of oo, is it possible to design a big project without any
need of dynamic_cast?

Thank you very much

Do you think it would be a good idea to neglect try and catch since
std::terminate would be called even without try and catch?

No, that's an exceptionally bad idea. std::terminate() calls std::abort(), which is a hard termination of the process; no destructors are called, no cleanup is performed, you're just pulling the plug.

Besides, in the world of oo, is it possible to design a big project without any
need of dynamic_cast?

Sure. Though in practice, the world isn't as elegant as theorists would like. While you can usually avoid dynamic_cast, sometimes it's necessary where good design and functional (ie. "efficient") software conflict. I can guarantee that given the choice between an elegant design and acceptable performance, wise programmers in the trenches will choose the latter.

Maybe the other choice is don't use inheritance if you don't need it?
Try another way, like static polymorphism, generic programming, procedural
programming, functional programming or other ways.

Edited 5 Years Ago by stereomatching: n/a

Maybe the other choice is don't use inheritance if you don't need it?

That's like saying that careless drivers cause accidents, so we shouldn't use cars.

Comments
Classic Narue wit. Brightened up my day!

That's like saying that careless drivers cause accidents, so we shouldn't use cars.

What I want to say is, use it wisely.
In used inheritance a lot, and I find out I don't need to use it so much.
Composition or static polymorphism is not a bad choice either.

This article has been dead for over six months. Start a new discussion instead.