using Macros: how can i get functions?

using macros, how can i get functions from a class name?
How do you mean? You want to pass the class name to a macro and get the name of a function, which function?
1
2
3
4
5
6
7
8
9
#define ClassWithEvents(DerivedClass,InstanceName) \
                class InstanceName : public DerivedClass {}InstanceName;

class a
{
    public:
    int b;
    void hello(); //adding these on macro
};

how can i get the hello() on macro?

using it:
1
2
3
4
5
ClassWithEvents(a, d);
void d::hello() // error: the 'hello' isn't member of 'd'
{
    cout << __PRETTY_FUNCTION__;
}
Last edited on
anyone knows the __PRETTY_FUNCTION__ macro declaration?
Doesn't __func__ give you the function name?
Last edited on
the __PRETTY_FUNCTION__ give me the class name and the function name. but i get a string. so don't resolve my problem. but i can't find their declaration for i see what i can use.
i need avoid that error that i showed commented
1
2
#define ClassWithEvents(DerivedClass,InstanceName) \
                class InstanceName : public DerivedClass {public: void hello();}; 


Also note that you put InstanceName at the end of the class, it creates a single instance with that name, so you would not be able to create another copy of the class in a very, very, very cryptic way.

also __PRETTY_FUNCTION__ doesn't exist in my compiler (mingw).
1
2
3
#ifndef __PRETTY_FUNCTION__
static_assert(false);
#endif // __PRETTY_FUNCTION__ 


Im not an expert for macros, but you should be aware that macro's are very very very stiff and they don't do anything special in the background. If you need this to be universal, you could have a 3rd parameter for what the function's name is.

Also kinda a shot in the dark but, wouldn't you desire to use polymorphic classes? Create 1 abstract class with virtual functions, and with that you can merge all the classes in one abstract list? That's how I handle my events. It doesn't have a pretty macro but its quite good. But then again from your example, you can get the same results by just using plain inheritance by just writing out your classes.

If you really want to be a bad boy, you could write something like this and make anyone who see it to hate you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define BAD_BOY_CLASS_INTERNALS public:\
                                int b; \
                                void hello();

class a
{
    BAD_BOY_CLASS_INTERNALS
};

#define ClassWithEvents(DerivedClass,InstanceName) \
                class InstanceName : public DerivedClass {BAD_BOY_CLASS_INTERNALS} InstanceName;

ClassWithEvents(a,b);

void b::hello()
{
    std::cout<<"woof!"<<std::endl;
}
__PRETTY_FUNCTION__ is handled specially by the compiler so you wont see it defined as regular macro.

If you search the GCC codebase for RID_PRETTY_FUNCTION_NAME (where RID stands for Reserved IDentifier) you'll see what I mean.
trying to remember... seems like compilers had some 'special' macros for telling you where you were (file name, function name, maybe scope ??) so you could build meta debugging macros. In visual studio some of the oddball functionality was in special macros called pragmas. When you start going down the rabbit holes, you can find what you need but it is usually not portable. The good news is that I think the not portable stuff looks like just pointless #defines to the compiler and are ignore?
yah a 'bad boy', but more tired writing everything. from 1 class to another :(
what i mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
class base
{
   public:
       void Hello();
};

class derived: public base
{
   public:
       //if i need redefine the Hello function
       //i must prototype again:
      void Hello();
};

why been a ''bad boy", if we can simplicate?
is what i mean.
thank you so much for all to all
Last edited on
can i add more functions prototypes?
1
2
3
#define events public:\
               void hello();\
               void Hi(); 

see the sample:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define events public:\
               void hello();\
               void Hi();

#define ClassWithEvents(DerivedClass,InstanceName) \
                class InstanceName : public DerivedClass {events}InstanceName;



class a
{
    public:
    int b;
};

ClassWithEvents(a, c);
ClassWithEvents(a, d);
void d::hello()
{
    cout <<"hello d";
}

void c::hello()
{
    cout <<"hello c";
}

void d::hi()
{
    cout <<"hi d";
}

void c::hi()
{
    cout <<"hi c";
}

i get 2 errors:
1 - "no 'void d::hi()' member function declared in class 'd'"
2 -"no 'void c::hi()' member function declared in class 'c'"
so what i'm doing wrong with 'events' macro?
i did a mistake without notice... the uppercase instead lowcase:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

#define events public: void hello();  void hi();

#define ClassWithEvents(DerivedClass,x,InstanceName) \
                class InstanceName : public DerivedClass {x}InstanceName;



class a
{
    public:
    int b;
};

ClassWithEvents(a,events, c);
ClassWithEvents(a,events, d);
void d::hello()
{
    cout <<"hello d\n";
}

void c::hello()
{
    cout <<"hello c\n";
}

void d::hi()
{
    cout <<"hi d\n";
}

void c::hi()
{
    cout <<"hi c\n";
}

int main()
{
    c.hello();
    d.hello();
    c.hi();
    d.hi();
    return 0;
}

works like i need. thank you so much for all to all... thank you
Overall macro's are a tool but its like goto, people sneer at it when you use it poorly. This is not how you use macros. You are using an oversized hammer to clean up 10 lines of code.
poteto: isn't good avoid repeating code lines?(like functions prototypes between derived class's)
the best way that i found was using macros.
i'm confused in 1 thing: if C++ is a C superset and it's always evolve, where are the properties and events for class's?
every functions from base class, must been repeated... so i avoid it in a nice way ;)
thank you so much
I don't know what you are trying to accomplish.

I don't need to explain it to you, just use your favorite search engine and read what people think of macros and why don't modern languages not have them. Spoiler: the cons outweigh the pros.

And also you are wrong, every function in a base class does not need to be repeated, it just depends whether it has a complete function or not.

And why mass produce classes with a macro, when you should be thinking why are you mass producing classes in the first place?
Can't understand your question ,are you referring on how to call a class?
Macros can definitely be useful. I don't think it's fair to compare them to gotos because you can almost always write better code using loops. There are a few problems with macros to be aware of but sometimes a macro is the most elegant, least error prone, solution. I'm not sure in this case though because I don't fully understand what you're trying to accomplish.
C++ is NOT a pure C superset. It almost is, but not exactly -- there are a few things in C that won't compile without small changes if you compile it as c++ code, for example malloc needs a cast in c++. Both languages are growing and changing, and they have diverged a bit as well.

macros are fine if not abused. For debugging, they can give you a ton of useful spew if you want to set all that up. A good rule of thumb is to only use them where nothing else can do the same job. There are only a very small number of things where that is true, most of them debugging type information about the code base itself that isnt available at run-time. Debuggers are so good these days that this extra info is dubious, but if you really wanted it...

I am also not really sure what you want to accomplish. Mass class production can be done without macros, and inheritance and other tools can avoid redundant code. I used to have a neural net library that cranked out C code and made thousands of generated structs in the process. It was unreadable, but it compiled and ran. /shrug. Code that writes code is challenging -- it rarely writes readable code, and it rarely writes efficient good code.




i'm sorry but i will try explain.
we create a class.. these class have several functions(or even virtual functions).
now if we create an instance of that class, we can't change the functions definitions.
if we use another class(that don't need have an instance, we must prototype the functions for we definition them on Global Scope.
so my 2 macros let me create a class from another class and let me change the functions definitions on Global Scope.
so, now, i found 1 limitation: i must define the functions, even if i don't need some of them.
maybe with time i can fix the problem ;)
why i must reprototype the functions, when i used them on base class?
yah it's a C++ rule. but the macros is helping reutilize the code ;)
thank you so much for all
Topic archived. No new replies allowed.