另一个复制构造函数问题 [英] another copy constructor question

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

问题描述




我想知道为什么在下面这段代码中,函数test1

在返回时调用一个拷贝构造函数,为什么test2没有。在一个函数中使用

的多个return语句是不是很好

编程风格?


感谢您的帮助


#include< iostream>


class foo {

public:

foo(){};

foo(const foo& c){std :: cout<< " COPU" <<的std :: ENDL; }

};


foo test1(){

if(true){

foo c;

返回c;

}

返回foo();

}


foo test2(){

foo c;

if(true){

}

返回c;

}


int main(无效){

std :: cout<< 测试1 << std :: endl;

test1();

std :: cout<< 测试2 << std :: endl;

test2();

返回0;

}

解决方案

ciccio写道:


我想知道为什么在下面这段代码中,函数test1

调用返回时的复制构造函数以及test2没有的原因。在一个函数中使用

的多个return语句是不是很好

编程风格?


感谢您的帮助


#include< iostream>


class foo {

public:

foo(){};

foo(const foo& c){std :: cout<< " COPU" <<的std :: ENDL; }

};


foo test1(){

if(true){

foo c;

返回c;

}

返回foo();

}


foo test2(){

foo c;

if(true){

}

返回c;

}


int main(无效){

std :: cout<< 测试1 << std :: endl;

test1();

std :: cout<< 测试2 << std :: endl;

test2();

返回0;

}



我相信在这种情况下,它归结为编译器能够优化复制的能力。在一种情况下,它可以,在另一种情况下它不能,

和那是关于它的。至于多个返回点的风格,它对用户来说已经达到了
。太多月以前我被教过结构化编程,

和单个返回点很重要。如今,如果您使用

RAII范例进行编程,多次退货就完全没问题,AFAICT。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


6月3日下午1:06,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:


ciccio写道:


我想知道为什么在下面这段代码中,函数test1

在返回时调用复制构造函数以及为什么test2才不是。在一个函数中使用

的多个return语句是不是真的很好

编程风格?


感谢您的帮助


#include< iostream>


class foo {

public:

foo(){};

foo(const foo& c){std :: cout<< " COPU" <<的std :: ENDL; }

};


foo test1(){

if(true){

foo c;

返回c;

}

返回foo();

}

< blockquote class =post_quotes>
foo test2(){

foo c;

if(true){

}

返回c;

}


int main(void){

std :: cout<< 测试1 << std :: endl;

test1();

std :: cout<< 测试2 << std :: endl;

test2();

返回0;

}



我相信在这种情况下,它归结为编译器能够优化复制的能力。在一种情况下,它可以,在另一种情况下它不能,

和那是关于它的。至于多个返回点的风格,它对用户来说已经达到了
。太多月以前我被教过结构化编程,

和单个返回点很重要。如今,如果您使用

RAII范例进行编程,多次退货就完全没问题,AFAICT。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问



看看Sutter解释它的下面的链接

http://www.gotw.ca/gotw/002.htm


注意问题不是关于性能,它只是

为什么在一种情况下构造函数被调用而在另一种情况下它不是。


AZanetti


ademirzanetti写道:


6月3日下午1点06分,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:


> ciccio写道:


>>我想知道为什么下面这段代码,函数test1
在返回时调用一个拷贝构造函数,为什么test2没有。一个函数中多个return语句的用法是不是很好的编程风格?
感谢您的帮助
#include< iostream>
class foo {
public:
foo(){};
foo(const foo& c){std :: cout<< " COPU" <<的std :: ENDL; }
};
foo test1(){
if(true){
foo c;
返回c;
}
返回foo ();
}
foo test2(){
foo c;
if(true){
}
返回c;
}
int main(void){
std :: cout<< 测试1 << std :: endl;
test1();
std :: cout<< 测试2 << std :: endl;
test2();
返回0;
}


我相信在这种情况下它归结为编译器''能够优化复制的能力。在一种情况下,它可以,在另一种情况下它不能,
和那是关于它。至于多个返回点的风格,对用户来说就是这样。太多时间以前,我被教过结构化编程,
并且单个返回点很重要。如今,如果您使用
RAII范例进行编程,多次返回就完全没问题,AFAICT。

V
-
请删除大写'A'时通过电子邮件回复
我不回复热门回复,请不要问



看看下面Sutter的链接解释它

http://www.gotw。 ca / gotw / 002.htm


请注意,问题不在于性能问题,而是在一个案例中构造函数为什么?b $ b被叫,而另一个则没有。


AZanetti



不确定你的观点是什么,或者你是否回复了我或OP.

也许你可以解释Sutter'的GotW#2的哪些部分适用于这里和

如何。非常感谢。


V

-

请在通过电子邮件回复时删除资金''A' br />
我没有回复最热门的回复,请不要问


Hi,

I was wondering why in the following piece of code, the function test1
calls a copy constructor at return and why test2 does not. Is the usage
of multiple return statements in one function not really a good
programming style?

Thanks for the help

#include <iostream>

class foo {
public:
foo() { };
foo(const foo &c) { std::cout << "copu" << std::endl; }
};

foo test1() {
if (true) {
foo c;
return c;
}
return foo();
}

foo test2() {
foo c;
if (true) {
}
return c;
}

int main(void) {
std::cout << "test 1" << std::endl;
test1();
std::cout << "test 2" << std::endl;
test2();
return 0;
}

解决方案

ciccio wrote:

I was wondering why in the following piece of code, the function test1
calls a copy constructor at return and why test2 does not. Is the usage
of multiple return statements in one function not really a good
programming style?

Thanks for the help

#include <iostream>

class foo {
public:
foo() { };
foo(const foo &c) { std::cout << "copu" << std::endl; }
};

foo test1() {
if (true) {
foo c;
return c;
}
return foo();
}

foo test2() {
foo c;
if (true) {
}
return c;
}

int main(void) {
std::cout << "test 1" << std::endl;
test1();
std::cout << "test 2" << std::endl;
test2();
return 0;
}

I believe in this case it comes down to the compiler''s ability to
optimise the copying away. In one case it can, in the other it cannot,
and that''s about it. As to the style of multiple return points, it''s up
to the user. Too many moons ago I was taught structured programming,
and a single return point was important. Nowadays if you program using
the RAII paradigm, multiple returns are perfectly OK, AFAICT.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jun 3, 1:06 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

ciccio wrote:

I was wondering why in the following piece of code, the function test1
calls a copy constructor at return and why test2 does not. Is the usage
of multiple return statements in one function not really a good
programming style?

Thanks for the help

#include <iostream>

class foo {
public:
foo() { };
foo(const foo &c) { std::cout << "copu" << std::endl; }
};

foo test1() {
if (true) {
foo c;
return c;
}
return foo();
}

foo test2() {
foo c;
if (true) {
}
return c;
}

int main(void) {
std::cout << "test 1" << std::endl;
test1();
std::cout << "test 2" << std::endl;
test2();
return 0;
}


I believe in this case it comes down to the compiler''s ability to
optimise the copying away. In one case it can, in the other it cannot,
and that''s about it. As to the style of multiple return points, it''s up
to the user. Too many moons ago I was taught structured programming,
and a single return point was important. Nowadays if you program using
the RAII paradigm, multiple returns are perfectly OK, AFAICT.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

Take a look on the link below where Sutter explain it

http://www.gotw.ca/gotw/002.htm

Note that the question is not regarding performance, it is just about
why in one case the constructor is called and in another it is not.

AZanetti


ademirzanetti wrote:

On Jun 3, 1:06 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

>ciccio wrote:

>>I was wondering why in the following piece of code, the function test1
calls a copy constructor at return and why test2 does not. Is the usage
of multiple return statements in one function not really a good
programming style?
Thanks for the help
#include <iostream>
class foo {
public:
foo() { };
foo(const foo &c) { std::cout << "copu" << std::endl; }
};
foo test1() {
if (true) {
foo c;
return c;
}
return foo();
}
foo test2() {
foo c;
if (true) {
}
return c;
}
int main(void) {
std::cout << "test 1" << std::endl;
test1();
std::cout << "test 2" << std::endl;
test2();
return 0;
}

I believe in this case it comes down to the compiler''s ability to
optimise the copying away. In one case it can, in the other it cannot,
and that''s about it. As to the style of multiple return points, it''s up
to the user. Too many moons ago I was taught structured programming,
and a single return point was important. Nowadays if you program using
the RAII paradigm, multiple returns are perfectly OK, AFAICT.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Take a look on the link below where Sutter explain it

http://www.gotw.ca/gotw/002.htm

Note that the question is not regarding performance, it is just about
why in one case the constructor is called and in another it is not.

AZanetti

Not sure what your point was or whether you replied to me or to the OP.
Perhaps you could explain what parts of Sutter''s GotW #2 apply here and
how. Much appreciated.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于另一个复制构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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