Question

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".

Was it helpful?

Solution

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.

OTHER TIPS

The compiler will replace instances of "my" with "me ->" when it compiles.

So presumably the intent is to dereference a pointer called "me". i.e. if the object pointed to has a method Dog(), instead of

me->Dog()

you can just use

my Dog()

if you will use my you will use me ->.

this will gives you an error unless me is defined somehow reasonable when my is used.

class B {
public:
    void f() {}
};


#define my me ->
int main(int argc, char** argv) {

    B* b = new B();
    b my f(); // error, expanded to: b me -> f()
    B* me = new B();
    my f(); // OK, expanded to: me -> f()
    delete b;
    delete me;
    return 0;
}

but:

#define me
#define my me ->
int main(int argc, char** argv) {
    B* b = new B();
    b my f(); //  OK, expanded to: b -> f();
    delete b;
    return 0;
}

This is a macro which makes my an alias for me ->.

The intended use seems to be so to allow usage of my x for any member x given that me is a pointer to some object. Hence, these two will be equivalent given that macro:

me->x
my x

Most likely, you also have a macro like this, somewhere in the code:

#define me this

which would let you use me instead of this in a member function. The intended effect is so that, in any member function of a class with member x, these two are equivalent:

my x
this->x

and it will unambiguously refer to the member x of the current instance, even if a local variable with the same name exists - because that's what this->x does.

That being said, this trick is horrible. It doesn't add anything which isn't already in the language - it only creates a new syntax for something which already has a well-defined and well-known syntax.

Also, by virtue of being a macro, it will very easily break perfectly valid code. For example,As soon as someone tries to declare a variable of type int called my, this macro could lead to very confusing error messages, as the actual code generated is:

// This is what is written:
int my;
// This is what it becomes:
int me ->;
// Unless the #define for me is available, in which case you get:
int this ->

My tip, and it seems from other comments as if there's a strong consensus: Avoid this macro and, if possible, delete it and replace all its uses by an ordinary member access.

The only reason I could think of to have such a macro would be in a translation layer for some other language in which me actually means the same as this in C++ and my is the same as this ->. So unless this is part of some lightweight language translation layer used on top of a C++ compiler, it needs to be removed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top