ProDev7
ProDev7

Reputation: 75

Concatenating my object with a string

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.

Upvotes: 0

Views: 60

Answers (2)

dzada
dzada

Reputation: 5874

The ideam is to create a class MyTypeNameList when using the macro like this Listof(MyTypeName)

It is a shortcut to create class named XXXXList declared with a default CTOR, and a member itsLength;

example in your code :

//declare a class 
Listof(A)

after preprocessor this is

class AList{ 
public:
   AList(){}
private: 
int itsLength; 
};

Upvotes: 2

AngelCastillo
AngelCastillo

Reputation: 2435

It is a Macro, this particular one expand into a definition of a class, for example

Listof(String)

Will expand to:

class StringList
{
  public:
    StringList(){}

  private:
    int itsLength;
}

That means that anywhere in your code where you use the macro is the same as writing the class itself.

Upvotes: 1

Related Questions