Search code examples
c++programming-languages

Creating a custom class through #define macro


I'm trying to create a macro with #define that'll allow me to create new class on demand. Here's my code:

#pragma once

#include "PDDefFileReader.h"

#define SET_LANG( lang ) *( new std::string( lang ) ) 

#define LANG( cName, lName )
class cName
{
public:
    cName()
    {
        _langName = SET_LANG( lName );                 <- HERE !!!!
        _reader = new PDDefFileReader( _langName );
    }
    ~cName(){}

    std::string Str(){ return _langName; }

private:
    PDDefFileReader* _reader;
    std::string _langName;
};

So I want to know how should I do it if I want the "lName" parameter of the define to be taken as a std::string object. For now (at the line "HERE !!!!") I get the error:

Error: Identifier "lName" is undefined

Any idea if what I want to do is possible ?

To give you some context, I'm doing a custom multi-language reader. So I could simply define new language by doing something like:

LANG( Cpp, "cpp" )
LANG( Perl, "pl" )

Thanks !!


Solution

  • Your macro is empty, by default it does not continue to the next line unless you use a \ at the very end, e.g.

    #define #define LANG( cName, lName ) \
    class cName \
    { \
        ...