Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++#定义我的自我->;_C++_C Preprocessor_Defined - Fatal编程技术网

C++ C++#定义我的自我->;

C++ C++#定义我的自我->;,c++,c-preprocessor,defined,C++,C Preprocessor,Defined,我正在浏览一个旧应用程序的源代码。 在这段代码中,我看到了很多“my”的用法 它被定义为 #define my me -> 但我不确定这到底意味着什么。 这是否意味着如果我使用“my”,它将使用“this->” 我知道这不是一个好的做法,但我需要了解它的作用 谢谢大家! 编辑: 以下是作者提供的更多信息: /* Use the macros 'I' and 'thou' for objects in the formal parameter lists (if the

我正在浏览一个旧应用程序的源代码。 在这段代码中,我看到了很多“my”的用法

它被定义为

#define my  me ->
但我不确定这到底意味着什么。 这是否意味着如果我使用“my”,它将使用“this->”

我知道这不是一个好的做法,但我需要了解它的作用

谢谢大家!

编辑:

以下是作者提供的更多信息:

/*
    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 ->
但是我仍然看不到“我”的定义。

在这件事上,
#define
非常简单:当你在你的代码中使用
my
时,它将被
me->
取代,所以代码是这样的

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;
实际上意味着

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

尽管这个技巧看起来很可爱,但对于熟悉C语法的读者来说,它会产生严重的误导。我强烈建议不要在代码中使用它。

如果您使用
my
,您将使用
me->

这将给您一个错误,除非在使用
my
时以某种合理的方式定义
me

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;
}
但是:


这是一个宏,它使
my
成为
me->
的别名

鉴于
me
是指向某个对象的指针,因此预期用途似乎是允许任何成员
x
使用
my x
。因此,鉴于该宏,这两个将是等效的:

me->x
my x
最有可能的是,代码中的某个地方也有这样一个宏:

#define me this
这将允许您在成员函数中使用
me
而不是
this
。预期效果是,在具有成员
x
的类的任何成员函数中,这两个函数是等效的:

my x
this->x
并且它将明确地引用当前实例的成员
x
,即使存在同名的局部变量,因为
this->x
就是这样做的

话虽如此,这个把戏太可怕了。它没有添加任何语言中没有的东西——它只是为已经有了定义良好的语法的东西创建了一个新的语法

此外,由于它是一个宏,它很容易破坏完全有效的代码。例如,当有人试图声明名为
my
int
类型的变量时,此宏可能会导致非常混乱的错误消息,因为实际生成的代码是:

// 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 ->
我的提示,从其他评论来看,似乎有一个强烈的共识:避免使用这个宏,如果可能的话,删除它并用普通成员访问替换它的所有用途


<> P>我想有这样一个宏的唯一原因是在其他语言的翻译层中,<代码> Me>代码>实际上意味着与<代码>相同。C++中的<<代码> > <代码>我的<代码>与<代码> > -><代码>相同。因此,除非这是C++编译器上使用的一些轻量级语言翻译层的一部分,否则需要删除它。

< P>编译器将在编译时,用“我-”替换“我”的实例。 因此,其目的大概是取消对名为“me”的指针的引用。i、 e.如果指向的对象具有方法Dog(),而不是

me->Dog()
你可以用

my Dog()

你所拥有的并不能定义我。它定义了
my
。(而且很可怕。)不管是什么,我都不会费心使用它……我打赌在其他地方有一个
#定义我这个
。如果可以的话,我会把它全部删除(用普通代码替换邪恶的宏)。对不起,我的意思是“我的”,而不是“我”。Post相应地改变了。wtf。帮你自己个忙,滚开。该代码的作者是临床上的疯子。我在帖子中添加了更多信息,也许这会让它更清楚。@tmighty哇!我认为这个定义很奇怪,但是另一个定义很邪恶!这种事没有什么坏处。使用预处理器在原始语言的基础上定义一种新语言是完全可以的,并且或多或少是它的全部功能。当被视为C语言时,它令人困惑,但事实并非如此:它是作者基于C语言的自定义语言。预处理器是在此类设计空间中使用的一个非常方便的工具。
my Dog()