c++ - How to automate class declarations? -


i have exceptions derived std::exception or std::runtime_error. method constructor explicit myexceptionx(const char *text = "") : std::exception(text) {}. there ways make code simpler without use of macro?

class myexception1: public std::exception { public:     explicit myexception1(const char *text = "") : std::exception(text) {} };  class myexception2: public std::exception { public:     explicit myexception2(const char *text = "") : std::exception(text) {} };  class myexception3: public std::exception { public:     explicit myexception3(const char *text = "") : std::exception(text) {} };  //... 

there's no need use class when public. can use struct instead. also, can inherit constructors:

struct myexception1: std::exception {     using std::exception::exception; };  struct myexception2: std::exception {     using std::exception::exception; };  struct myexception3: std::exception {     using std::exception::exception; }; 

also, if need different types, can this:

template <int> struct myexception : std::exception {     using std::exception::exception; };   using myexception1 = myexception<1>; using myexception2 = myexception<2>; using myexception3 = myexception<3>; 

you can use enum instead of int if want more descriptive names.


Comments