bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exception classes, multiple inheritence


From: Günter Neiß
Subject: Re: exception classes, multiple inheritence
Date: Tue, 06 Feb 2001 13:04:42 +0100

Dean Hoover schrieb:
> 
> I am attempting to implement some exception object classes
> like the std ones and actually derived from them, but with the
> optional ability to carry extra debugging information
> (__FILE__, __LINE__)
> 
> I am including what I have so far at the end of this note.
> I would like to be able to use these classes in libraries
> I am writing, and have the users be able to either catch
> these classes or anything appropriate from std, as they
> choose. If they use my extended classes, they may be
> able to get file and line information, if it was provided at
> throw. I also am writing little helper macros so that the
> __FILE__ and __LINE__ arguments are automatically
> provided if DEBUG is defined. This way, one of these
> objects can be thrown explicitly, or through the macro,
> depending on the desired outcome.
> 
> Have any of you tried something like this before? Can
> you offer an alternative approach? When I run the
> code, it dumps core. I am using gcc 2.95.2 on linux.
> Any ideas what's wrong? Is it my code or the compiler's
> treatment of a thrown object with multiple inheritence?
> 
> Thanks in advance.
> 
> Dean
> 
> Here it is:
> 
> #define DEBUG
> 
> #include <cstdio>
> #include <cstring>
> #include <stdexcept>
> #include <string>
> 
> namespace gutils {
> 
> class Exception : public std::exception
> {
> public:
>     Exception
>     (
>       const string& what_arg,
>       const char* file = 0,
>       unsigned long line = 0
>     ) : e_what_(what_arg), file_(0), line_(line)
>     {
>         if (file)
>             file_ = new std::string(file);
>     }
Mixing 'string' and 'char *'  isn't a good idea !
OK, rewrite it:

class Exception : public std::exception
 {
 public:
     Exception( const string & what_arg, const string & file =
std::string( "" ), unsigned long line = 0 ) 
      : e_what_( what_arg ), file_( file ), line_( line )
      {}
     virtual ~Exception() {}

     virtual const char * what() const { return e_what_.c_str(); }
     virtual const char * file() const { return file_.c_str(); }
     virtual unsigned long line() const { return line_; }
 
 private:
     std::string e_what_;
     std::string file_;
     unsigned long line_;
 }; // class Exception
 
#ifdef DEBUG
  #define EXCEPTION(WHAT) gutils::Exception((WHAT), std::string(
__FILE__ ), __LINE__)
 #else
  #define EXCEPTION(WHAT) gutils::Exception((WHAT))
#endif

  greetings
    Günter

Attachment: smime.p7s
Description: Kryptographische Unterschrift mit S/MIME


reply via email to

[Prev in Thread] Current Thread [Next in Thread]