Can't use macro define class in C++

2020-07-18 10:32发布

问题:

I want to generate many sub-class which only has little difference, so I want to use macro to simplify my job. The macro define below:

#define DECLARE_SUB_CLASS(sub_class_name, base_class_name, value1) \
class sub_class_name:base_class_name \
{ \
public: \
    virtual int initialize(const void *); \
    virtual int run(const void *); \
    virtual void reset(); \
    virtual int output(const char*); \
    virtual void terminate(); \
private: \
    static const char m_szValue=#value1; \
};

I use it like this:

DECLARE_SUB_CLASS(RTCount13, RTCountBase, 13);

when I compiling with VC2005, it say

error C2065: 'RTCount13' : undeclared identifier

what's the problem?

回答1:

Use gcc -E (or similar for other preprocessor)

gcc -E prepro.cxx

# 1 "prepro.cxx"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "prepro.cxx"
# 17 "prepro.cxx"
class RTCount13:RTCountBase { 
  public: 
  virtual int initialize(const void *); 
  virtual int run(const void *); 
  virtual void reset(); 
  virtual int output(const char*); 
  virtual void terminate(); 
  private: 
  static const char m_szValue="13"; 
};;

You try to assign "13" to a char.

By the way you could also use a template instead of a macro to do the exact same thing your macro did (namely to declare but not define the methods). Here's a complete (trimmed down) example with separate method definitions.

#include <iostream>
class RTCountBase {};

template <class base_class_name, int v>
class RTCount: base_class_name { 
  public: 
   virtual int output(); 
   virtual void terminate(); 
  private: 
   static const int m_szValue=v; 
};

template <class base_class_name, int v>
int RTCount<base_class_name,v>::output(){ return m_szValue; }

template <class base_class_name, int v>
void RTCount<base_class_name,v>::terminate(){ std::cout <<" term "<<std::endl; }

typedef RTCount<RTCountBase,13> RTCount13; // typedef instead of macro
typedef RTCount<RTCountBase,14> RTCount14;

int main(){
   RTCount13 myc13;
   RTCount14 myc14;
   std::cout << "my13: "<<myc13.output()<<std::endl;
   std::cout << "my14: "<<myc14.output()<<std::endl;
   return 0;
}


回答2:

Since you're using C++, you really should avoid using #define to do this. The C++ language has better ways to do this, for example:

template <class B, const char C>
class SomeClass : public B
{
public:
  virtual int initialize(const void *) {return m_szValue;}
  virtual int run(const void *) {return 0;}
  virtual void reset() {}
  virtual int output(const char*) {return 0;}
  virtual void terminate() {}
private: 
  static const char m_szValue=C; 
};

and then change this:

DECLARE_SUB_CLASS(RTCount13, RTCountBase, 13);

RTCount13 some_instance;

to:

SomeClass <RTCountBase, 13> some_instance;


回答3:

You're missing a hash sign when defining your static char:

static const char m_szValue=#value1;

instead of

static const char m_szValue=##value1;

EDIT:

If m_szValue is meant to be a string, you need to define it outside the class declaration. I suggest you define a macro for this also to keep consistency:

#define DECLARE_SUB_CLASS(sub_class_name, base_class_name) \
class sub_class_name:base_class_name \
{ \
    //.....\
private: \
    static const char* m_szValue; \
};

#define DEFINE_SUB_CLASS_STATIC(sub_class_name, value1) \
    const char* sub_class_name::m_szValue = #value1; \

which you use like this:

//header file
DECLARE_SUB_CLASS(RTCount13, RTCountBase);

//impl file:
DEFINE_SUB_CLASS_STATIC(RTCount13, 13);