Jump to content
  • Advertisement
Sign in to follow this  
Rip7

Copy reference of a derived class

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a catch function witch accepts an Exceptions object as argument. Now this Exceptions class is an abstract base class for GeneralError, ErrorWarning, ... Now I want to wrap the catched exception with a temperature reading and rethrow it again. But the problem is that after the catch the Exceptions object is deleted and I don't now how to make a copy of the Exceptions reference object so that it holds the right derived class. catch (Exceptions &error) { throw ErrorPacket (error, Temperature); } How can I solve this, is there a method to make a copy of error witch holds the right derived class ? P.S: For the people who are interested, this is for a wireless temperature sensor. http://rip7.servehttp.com/scoop.jpg

Share this post


Link to post
Share on other sites
Advertisement
couldn't you use dynamic_cast to figure out which derived class it belongs to? Also, if you don't want to enable RTTI maybe you could use some other means from within your base class to allow it to know which class it belongs to (an enumeration or something to let you know which exception class the base pointer really is pointing at) Then you just cast the base pointer appropriately and use the derived class's assignment operator

hth
moe.ron

Share this post


Link to post
Share on other sites
Alternatively have a pure virtual clone() function in the base class and override it in each derived class along the lines of:

Exceptions* GeneralError::clone()
{
return new GeneralError(*this);
}

And then throw the error packet with the newly allocated copy.

Share this post


Link to post
Share on other sites
The clone() function option is the best idea for my problem I think. But just for curiosity how would you use dynamic_cast in this situation ?

Share this post


Link to post
Share on other sites
dynamic_cast returns zero if the cast to the specified type isn t possible

so you can abuse its return value and perform several casts until you found a possible cast target


but id suggest you use the virtual clone method its straight forward and easy to implement

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
dynamic_cast returns zero if the cast to the specified type isn t possible


returns zero for pointers types and throws std::bad_cast for reference types.

@Rip7 - To be honest what your doing is horrid even the idea of using virtual constructor idiom is flawed and has issues using it in exception handling, i think you should rethink what exceptions really are and rethink your design.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!