Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ #define my me ->

I am going through the source of an older application. In this code I see a lot of the usage "my".

It was defined as

#define my  me ->

But I am unsure what exactely that means. Does that mean that if I use "my", it will use "this->"?

I know it is not a good practice, but I need to understand what it does.

Thank you!

Edit:

Here is more info from the author:

/*
    Use the macros 'I' and 'thou' for objects in the formal parameter lists
    (if the explicit type cannot be used).
    Use the macros 'iam' and 'thouart'
    as the first declaration in a function definition.
    After this, the object 'me' or 'thee' has the right class (for the compiler),
    so that you can use the macros 'my' and 'thy' to refer to members.
    Example: int Person_getAge (I) { iam (Person); return my age; }
*/
#define I  Any void_me
#define thou  Any void_thee
#define iam(klas)  klas me = (klas) void_me
#define thouart(klas)  klas thee = (klas) void_thee
#define my  me ->
#define thy  thee ->
#define his  him ->

But I still cannot see the definition of "me".

like image 598
tmighty Avatar asked Mar 21 '23 11:03

tmighty


1 Answers

The #define is very straightforward in this matter: when you use my in your code, it will be substituted by me ->, so the code like this

struct X {
    char first_name[100];
    char last_name[100];
    int age;
} *me;

me = malloc(sizeof(struct X));
strcpy(my first_name, "John");
strcpy(my last_name, "John");
my age = 23;

will actually mean

strcpy(me->first_name, "John");
strcpy(me->last_name, "John");
me->age = 23;

Although this trick may look cute, it is grossly misleading to readers familiar with the syntax of C. I would strongly recommend against using it in your code.

like image 52
Sergey Kalinichenko Avatar answered Apr 06 '23 04:04

Sergey Kalinichenko