Name class with version variable in

Hi, I´m making a class with C++ an I need introduced the version of this class in the class name, I don't know if it is possible but... Can I create the class name with a variable or with one define?

myclass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#define CLASSNAME  "MyClass_1"

class CLASSNAME 
{
    CLASSNAME();
    ~CLASSNAME();

    void function1();
    void function2();
    .
    .
    .
}


myclass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CLASSNAME::CLASSNAME()
{
    ...
}

CLASSNAME::~CLASSNAME()
{
    ...
}

void CLASSNAME::function1()
{
    ...
}
void CLASSNAME::function2()
{
    ...
}


Is it posible?

thanks!!!
Last edited on
1
2
3
4
5
6
7
template < unsigned int VERSION > class my_class ;

template <> class my_class<1>
{
     my_class::my_class( /* ... */ ) ;
     // etc 
};


Using #define just means that the preprocessor substitutes the text contained in the macro definition for the macro name, wherever it finds it, before the compiler compiles the code.

So if the text substitution results in legal C++ then, yes, it's possible. In this case, substituting "MyClass_1" would result in:

1
2
3
4
5
6
7
8
9
10
11
class "MyClass_1"
{
    "MyClass_1"();
    ~"MyClass_1"();

    void function1();
    void function2();
    .
    .
    .
}


which would not be legal C++.

Using #define CLASSNAME MyClass_1

would result in:

1
2
3
4
5
6
7
8
9
10
11
class MyClass_1
{
    MyClass_1();
    ~MyClass_1();

    void function1();
    void function2();
    .
    .
    .
}

which is legal.

I'm not sure what you're really going to gain from this, though. If you're worried about version control of your software, use a proper version control system - Subversion is free, if you don't want to pay for something sophisticated - instead of obscuring your code.
Thank you very much for both for responding so quickly, you have solved a very big problem. I use MikeyBoy solution, I quip out " from the #define and the compiler substituted the defined variable and the project compile correctly.

Thanks a lot.


Topic archived. No new replies allowed.