vendredi 29 novembre 2019

Macro merge c++? [duplicate]

This question is an exact duplicate of:

I would like to know if it was possible to merge several macros in C++

Let me explain : I have an A macro that does not do anything at all. I want to add a macro B to A so that A = A + B, then I want to add a macro C to A so that A becomes A = A + C, and so that A contains B and C. I also want these operations to be done in different .h files and not in a single file like that :

// in Component.h
#define A
class Component
{

}
class myXmlParser
{
    template <class T>
    void addComponent<T>()
    {
        myComponents.push_back(new T);
    }

    std::vector<Component> myComponents;
}


// in PlayerScript.h
#include "Component.h"
class Player : public Component
{ 
#define A \
myParser.addComponent<Player>();
}

// in Enemy.h
#include "Component.h"
class Enemy : public Component
{
#define A \
myParser.addComponent<Enemy>();
}

// in main.cpp
void main()
{
    myXmlParser myParser;
    A
}

and the output is :
void main()
{
    myXmlParser myParser;
    myParser.addComponent<Player>();
    myParser.addComponent<Enemy>();
}

Now, if the user need to add another script, he dont need to touch the main, only his script.

Thank you ! Please !

Aucun commentaire:

Enregistrer un commentaire