Foo :: Foo() Foo :: Foo(int) Foo ::〜Foo() Foo ::〜Foo() 我不知道,this->有什么不同。应该这样做。 顺便说一句:在构造函数中初始化i没有帮助(虽然程序 可能有UB但没有这样做)。 这看起来像是gcc中的一个bug(在我的例子中是4.3.0)。 反汇编显示Foo( i)编译成Foo()。 ----- BEGIN PGP SIGNATURE ----- 版本:GnuPG v1.4.9(GNU / Linux) iEYEABECAAYFAkj6ufEACgkQx9p3GYHlUOK + qACdFG4hsTUFnj OplpkCJfQQYkeO m1wAn0sgSHrh78cUi1IK + NFFFvNFFtFl = qVU9 - ---结束PGP签名----- 嗨 我在VS 2005中运行了你的代码,我得到了以下输出 无限期: Foo( ) Foo() .... 当然,编译器发出以下警告: 警告C4717:''Foo :: Foo'':递归所有控制路径,函数 将导致运行时堆栈溢出 如Kai写的,如果我们改变 Foo(i); to Foo(this-> i); 我们得到了预期输出: Foo :: Foo() Foo :: Foo(int) Foo ::〜Foo() Foo ::〜Foo() 有一个侧面点:有一些建议不要打电话 a建设者里面另一个构造函数的主体: http://www.parashift.com/c+ + -faq-lit .... html#faq-10.3 问候, Saeed Amrollahi Hi,This code is of no use. But I am just curious to know about whathappening here.#include <iostream>using namespace std;class Foo {private:int i;public:Foo() {cout << "Foo::Foo()" << endl;Foo(i);}Foo(int i) : i(i) {cout << "Foo::Foo(int)" << endl;}~Foo() {cout << "Foo::~Foo()" << endl;}};int main(int argc, char *argv[]){Foo();}I am getting infinite call to "Foo::Foo()". Program is not evenexecuting "Foo::Foo(int)".Vijay 解决方案Hm, I am flabbergasted. The following _does_ the expected:Foo() {cout << "Foo::Foo()" << endl;Foo( this->i );}and prints:Foo::Foo()Foo::Foo(int)Foo::~Foo()Foo::~Foo()I have no idea, what difference the "this->" should make.BTW: initializing i in the constructor does not help (although the programmight have UB without doing so).BestKai-Uwe BuxHm, I am flabbergasted. The following _does_ the expected: Foo() { cout << "Foo::Foo()" << endl; Foo( this->i ); }and prints: Foo::Foo() Foo::Foo(int) Foo::~Foo() Foo::~Foo()I have no idea, what difference the "this->" should make.BTW: initializing i in the constructor does not help (although the programmight have UB without doing so).This looks like a bug in gcc (4.3.0, in my case).Dissassembly shows that Foo(i) gets compiled into Foo().-----BEGIN PGP SIGNATURE-----Version: GnuPG v1.4.9 (GNU/Linux)iEYEABECAAYFAkj6ufEACgkQx9p3GYHlUOK+qACdFG4hsTUFnj OplpkCJfQQYkeOm1wAn0sgSHrh78cUi1IK+NFFFvNFFtFl=qVU9-----END PGP SIGNATURE-----HiI ran your code in VS 2005 and I got the following outputindefinitely:Foo()Foo()....of course, compiler issued the following warning:warning C4717: ''Foo::Foo'' : recursive on all control paths, functionwill cause runtime stack overflowas Kai wrote, if we changeFoo(i);toFoo(this->i);we get the expected output:Foo::Foo()Foo::Foo(int)Foo::~Foo()Foo::~Foo()There is a side point: There are some recommendations that don''t calla constructor inside the body of another constructor: http://www.parashift.com/c++-faq-lit....html#faq-10.3Regards,Saeed Amrollahi 这篇关于查询构造函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 13:25