简体   繁体   English

G++ 编译器错误?

[英]G++ Compiler bug?

Here is what my code looks like which is very simple:这是我的代码,非常简单:

#include <stdio.h>

class Test
{
public:
   Test()
   {
      printf ("contructor !\n");
   }

   ~Test()
   {
      printf ("destructor !\n");
   }

   Test(const Test& test)
   {
      printf ("copy contructor !\n");
   }
};


int main()
{
    Test xyz(xyz);
    return 0;
}

Then I type g++ a.cpp;然后我输入 g++ a.cpp; ./a.out ./a.out

It output: copy contructor !它输出:复制构造函数! destructor !析构函数!

but no contructor output !但没有构造函数输出!
I`m confused, is it a bug of compiler?我很困惑,这是编译器的错误吗?

see pic看图片

In the line where you create the object xyz the address of this object in known even before the object is created.在创建对象xyz的行中,甚至在创建对象之前就知道该对象的地址。 That means that you may take the reference to this (not created yet) object and pass it as a parameter to the constructor of this object itself.这意味着您可以引用此(尚未创建的)对象并将其作为参数传递给此对象本身的构造函数。 From the compiler point of view it is possible.从编译器的角度来看,这是可能的。 You don't initialize any fields, so you don't observe any fields initialized with junk.您没有初始化任何字段,因此您不会观察到任何用垃圾初始化的字段。 Anyway, you get what you deserve.不管怎样,你得到了你应得的。

Is that just a theoretical question or you really have a practical purpose?这只是一个理论问题还是您真的有实际目的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM