我很惊讶地意外地发现下面的作品:
#include <iostream>
int main(int argc, char** argv)
{
struct Foo {
Foo(Foo& bar) {
std::cout << &bar << std::endl;
}
};
Foo foo(foo); // I can't believe this works...
std::cout << &foo << std::endl; // but it does...
}
我将构造对象的地址传递给它自己的构造函数。这看起来像是源代码级别的循环定义。这些标准真的允许你在构造对象之前就把对象传递给函数吗? 或者这是一个未定义行为?
I suppose it's not that odd given that all class member functions already have a pointer to the data for their class instance as an implicit parameter. And the layout of the data members is fixed at compile time.
注意,我并不是在问这是否有用或者是个好主意; 我只是在到处修修补补,以便更多地了解课程。