查询构造函数调用 [英] Query with constructor calls

查看:90
本文介绍了查询构造函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



此代码没用。但是我很想知道

在这里发生了什么。


using namespace std;


class Foo {

private:

int i;

public:

Foo(){

cout<< "富::美孚()" << endl;

Foo(i);

}

Foo(int i):i(i){

cout<< "富::美孚(INT)" << endl;

}

~Foo(){

cout<< "富::〜美孚()" <<结束;

}

};


int main(int argc,char * argv [])

{

Foo();

}


我无限期地调用Foo :: Foo( )" ;.程序甚至不执行
执行Foo :: Foo(int)。


Vijay

解决方案

Vijay Meena写道:




这段代码没用。但是我很想知道

在这里发生了什么。


using namespace std;


class Foo {

private:

int i;

public:

Foo(){

cout<< "富::美孚()" << endl;

Foo(i);

}

Foo(int i):i(i){

cout<< "富::美孚(INT)" << endl;

}

~Foo(){

cout<< "富::〜美孚()" <<结束;

}

};


int main(int argc,char * argv [])

{

Foo();

}


我无限期地调用Foo :: Foo( )" ;.程序甚至没有执行Foo :: Foo(int)的



嗯,我很惊讶。以下_does_预期:


Foo(){

cout<< "富::美孚()" << endl;

Foo(this-> i);

}


和打印:

Foo :: Foo()

Foo :: Foo(int)

Foo ::〜Foo()

Foo ::〜Foo()


我不知道,this->有什么不同。应该这样做。


BTW:在构造函数中初始化我没有帮助(虽然程序

可能没有UB而没有这样做)。
< br $>
最佳


Kai-Uwe Bux


Kai-Uwe Bux写道:
< blockquote class =post_quotes>
Vijay Meena写道:


>
此代码没用。但我很想知道这里发生了什么。

#include< iostream>
使用命名空间std;

类Foo {<私人:
int i;
公开:
Foo(){
cout<< "富::美孚()" <<结束;
Foo(i);
}
Foo(int i):i(i){
cout<< "富::美孚(INT)" << endl;
}
~Foo(){
cout<< "富::〜美孚()" << endl;
}
};

int main(int argc,char * argv [])
{
Foo();
}

我无限期地调用Foo :: Foo()。程序甚至没有执行Foo :: Foo(int)。



嗯,我很惊讶。以下_does_预期:


Foo(){

cout<< "富::美孚()" << endl;

Foo(this-> i);

}


和打印:

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签名-----


10月19日,6:32 * am,Vijay Meena< vijay.me ... @ gmail.comwrote:




此代码没用。但是我很想知道

在这里发生了什么。


using namespace std;


class Foo {

private:

* * * * int i;

public:

* * * * Foo(){

* * * * * * * * cout<< "富::美孚()" << endl;

* * * * * * * * Foo(i);

* * * *}

* * * * Foo( int i):i(i){

* * * * * * * * cout<< "富::美孚(INT)" << endl;

* * * *}

* * * * ~Foo(){

* * * * * * * * cout< ;< "富::〜美孚()" <<结束;

* * * *}

};


int main(int argc,char * argv [])

{

* * * * Foo();


}


我无限期地调用Foo :: Foo()。程序甚至不执行
执行Foo :: Foo(int)。


Vijay






我在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 what
happening 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 even
executing "Foo::Foo(int)".

Vijay

解决方案

Vijay Meena wrote:

Hi,
This code is of no use. But I am just curious to know about what
happening 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 even
executing "Foo::Foo(int)".

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 program
might have UB without doing so).

Best

Kai-Uwe Bux


Kai-Uwe Bux writes:

Vijay Meena wrote:

>Hi,
This code is of no use. But I am just curious to know about what
happening 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 even
executing "Foo::Foo(int)".


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 program
might 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 OplpkCJfQQYkeO
m1wAn0sgSHrh78cUi1IK+NFFFvNFFtFl
=qVU9
-----END PGP SIGNATURE-----


On Oct 19, 6:32*am, Vijay Meena <vijay.me...@gmail.comwrote:

Hi,
This code is of no use. But I am just curious to know about what
happening 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 even
executing "Foo::Foo(int)".

Vijay


Hi

I ran your code in VS 2005 and I got the following output
indefinitely:
Foo()
Foo()
....

of course, compiler issued the following warning:
warning C4717: ''Foo::Foo'' : recursive on all control paths, function
will cause runtime stack overflow
as Kai wrote, if we change
Foo(i);
to
Foo(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 call
a constructor inside the body of another constructor:
http://www.parashift.com/c++-faq-lit....html#faq-10.3

Regards,
Saeed Amrollahi


这篇关于查询构造函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆