0

I am stuck with this example:

#define Listof(Type) class Type##List \
   { \
   public: \
   Type##List(){} \
   private: \
   int itsLength; \
   };

Could anyone explain to me what is the intention and the point in this example? Because I found this example in a book but it was not explained.

4

2 回答 2

2

想法是在使用这样的宏时创建一个类 MyTypeNameListListof(MyTypeName)

它是创建名为 XXXXList 的类的快捷方式,该类声明为默认 CTOR 和成员 itsLength;

您的代码中的示例:

//declare a class 
Listof(A)

在预处理器之后这是

class AList{ 
public:
   AList(){}
private: 
int itsLength; 
};
于 2013-09-02T21:27:57.913 回答
1

它是一个Macro,这个特定的扩展为一个类的定义,例如

Listof(String)

将扩展为:

class StringList
{
  public:
    StringList(){}

  private:
    int itsLength;
}

这意味着在代码中使用宏的任何地方都与编写类本身相同。

于 2013-09-02T21:32:00.837 回答