Avoid slicing of exception types (C++) -


I am preparing an exception hierarchy in my library for C ++. "Hierarchy" are 4 classes derived from std :: runtime_error I would like to avoid exception classes so that the copy constructor can be preserved. But apparently GCC requires the copy maker to throw their names, so the complainant about the secure copy constructor is complained. Visual C ++ 8.0 compiles exactly the same code. Is there any portable way to reduce the skiing problem for exception classes? Does the standard say anything about whether any implementation may require copy constructor of a class, which should be thrown away?

I want to clear the exception hierarchy from your library separately. std :: exception Hierarchy as much as possible, and always get some exceptions from within that hierarchy. You may want to read, read, and read in particular. "Forcing Users to Catch By Context" , I'm not aware of it a well. The only way is that I have come up with an hour or play (this is Sunday afternoon):

  square foo_exception {public: clear foo_exception (std :: string msg_): M_msg (Msg_) {} Virtual ~ foo_exception () {} Virtual Zero Extends () {Throw * This; } Virtual std :: string const & amp; Msg () const {returns m_msg; } Conserved: foo_exception (foo_exception; cons and other): m_msg (other .mmsg) {} private: std :: string m_msg; }; Class bar_exception: public foo_exception {public: clear bar_exception (std :: string msg_): foo_exception (msg_), m_error_number (errno) {} virtual zeros increment () {throw * this; } Int error_number () const {return m_error_number; } Conserved: bar_exception (bar_exception const and others): foo_exception (other), m_error_number (other.m_error_number) {} Private: int m_error_number; };  

The idea is to protect the copy guardian and class (arges) instead of .raise () instead of throw class (args) < / Code>. This allows you to throw a plural form bound exception, which your users can only hold by reference. Any effort to catch with value should be made with a good compiler warning. Something like this:

foo.cpp: 59: Error: 'bar_exception :: bar_exception (const bar_exception & amp;)' is protected

foo.cpp: 103: Error: This reference inside

Of course it comes at all costs because now you can not explicitly use throw or you can with a similar compiler warning Welcome to:

foo.cpp: in function 'void h ()':

foo.cpp: 31: Error: 'foo_exception :: foo_exception (const foo_exception & Amp;) 'is protected

foo.cpp: 93: error:

foo.cpp: 31: error in this context:' foo_exception :: foo_ Exception (const foo_exception & amp;) 'is protected

Foo.cpp: 93: error: in this context

Overall, I rely on coding standards and documents I do not want you to always hold context. Ensure that your library captures exceptions that handle it by reference and throws fresh objects (e.g., class (constructor arrogs) or throw ) . I would expect other C ++ programmers to keep the same knowledge - but to make sure to add a note to any document.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -