Hello again. 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? beacause I found this example in a book but not explained.

Where did this come from? I have not seen coding like this, at all.

It's old school templating before C++ supported (or widely supported) templates. I'd question how old your book is if that's an example. It must be at least 20 years old. Regardless, not a book you should be learning modern C++ from unless that example is prefixed, suffixed, and bordered with "DO NOT DO THIS".

That is an absolutely terrible example, it should never appear in a book, or at least, not one worth reading (unless it is presented as an example of what not to do).

In any case, what this MACRO is trying to do is automatically generate a list of objects of a particular type. For example, if you have some class called Foo and you use that MACRO as Listof(Foo), it will expand to this:

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

Or, I imagine there was more to it than that, but you get the point, it generates a complete declaration of a class FooList to represent a list of Foo objects.

In reality, C++ has a native feature for this kind of thing, it is called templates. You would normally write this instead:

template <typename T>
class List { 
  public: 
    List(){} 
  private: 
    int itsLength; 
};

and then use it as List<Foo>. This is how standard containers like std::vector and std::list are made.

Any book that recommends or shows this MACRO technique is either extremely old and written by a grossly incompetent programmer.

Hello Guys! I forgot to tell you that this book just explained the CONCATENATION operator and it is exlpaining the preference of TEMPLATES rather than MACRO. what I want now: after you create FooList, do you mean that Foo is another class??? if so how could you use and initialize its objects in FooList??? for example I have 5 objects of type Foo how to manage them in FooList???? Any help is highly appreciated.
Note: i just want to know how could I make the example work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.