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;
} 12 1316
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...@com Acast.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...@com Acast.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
On Jun 3, 6:06 pm, Victor Bazarov <v.Abaza...@com Acast.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.
In the end, yes. Probably, the compiler sees that there are
branches which don't return c, and so does not apply NVRO,
although it conceivably could, because in every case c is
constructed, it is the return value.
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.
If you don't care about readable or correct code.
In practice, functions should be small enough that the impact on
readability or correction are minor; there are certainly larger
issues. And there are also special cases where it is
acceptable, or maybe even preferred (a function which consists
of a single switch, with a return in each case). But as a
general rule, its better to avoid.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Hi!
James Kanze schrieb:
In practice, functions should be small enough that the impact on
readability or correction are minor; there are certainly larger
issues. And there are also special cases where it is
acceptable, or maybe even preferred (a function which consists
of a single switch, with a return in each case). But as a
general rule, its better to avoid.
Reminds me of the ugly:
int foo(int param)
{
int result;
if(bar(param))
{
result = 10;
}
else
{
result = -5;
}
return result;
}
there is mode code necessary to handle the SESE (single entry, single
exit) than there is for actual behaviour. I can't stand it. It's not
readable in my opinion.
Frank
In article <6a************ *@mid.dfncis.de >, bl************@ gmx.net
says...
[ ... ]
int foo(int param)
{
int result;
if(bar(param))
{
result = 10;
}
else
{
result = -5;
}
return result;
}
there is mode code necessary to handle the SESE (single entry, single
exit) than there is for actual behaviour. I can't stand it. It's not
readable in my opinion.
This can be made SESE with no flow-control at all:
int foo(int param) {
int rets[-5, 10];
return rets[(bool)bar(param )];
}
No code for anything but the values and the behavior.
--
Later,
Jerry.
The universe is a figment of its own imagination.
In article <Zc************ *************** ***@posted.comn et>, al***@start.no says...
* Jerry Coffin:
[ ... ]
int foo(int param) {
int rets[-5, 10];
return rets[(bool)bar(param )];
}
Uhm, I think you must have been coding in some other language recently... ;-)
Nope -- my fondness for such things isn't recent at all. To a large
extent it goes back to my days writing Fortran. Its (nearly) complete
lack of control structures made such code seem quite reasonable.
Anyways, as implicit in my article else-thread, I'd simply use a conditional
expression rather than a look-up.
That's certainly an option, of course. I'll openly admit I'm fond of the
table driven version, quite possibly to an inordinate degree.
That is, if I chose to make this a function at all.
Well yes, there is that. As it stands right now, it looks like a pretty
worthless function, but it was clearly written more to make a point than
with any real purpose in mind. The problem is that in many cases, by the
time you make it useful, the point he was trying to make is like to
disappear.
--
Later,
Jerry.
The universe is a figment of its own imagination.
On Jun 5, 12:42 am, Frank Birbacher <bloodymir.c... @gmx.netwrote:
James Kanze schrieb:
In practice, functions should be small enough that the impact on
readability or correction are minor; there are certainly larger
issues. And there are also special cases where it is
acceptable, or maybe even preferred (a function which consists
of a single switch, with a return in each case). But as a
general rule, its better to avoid.
Reminds me of the ugly:
int foo(int param)
{
int result;
if(bar(param))
{
result = 10;
}
else
{
result = -5;
}
return result;
}
In that case, the obvious way to write the function is:
int
foo( int param )
{
return bar( param ) ? 10 : -5 ;
}
Anything else is obfuscation.
If the branches become more complex, however, then using the
result variable certainly improves readability.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Jun 5, 2:13 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <6aok2tF38n2s.. .@mid.dfncis.de >, bloodymir.c...@ gmx.net
says...
[ ... ]
int foo(int param)
{
int result;
if(bar(param))
{
result = 10;
}
else
{
result = -5;
}
return result;
}
there is mode code necessary to handle the SESE (single entry, single
exit) than there is for actual behaviour. I can't stand it. It's not
readable in my opinion.
This can be made SESE with no flow-control at all:
int foo(int param) {
int rets[-5, 10];
I presume that here you meant:
static int const rets[] = { -5, 10 } ;
return rets[(bool)bar(param )];
}
No code for anything but the values and the behavior.
I don't know. I often use something similar instead of a
switch. But somehow the idea of indexing with "bool" doesn't
appeal to me---you're really counting on something that is only
present for hister^H^Horica l reasons: the fact that conversion
of bool to int is guaranteed to return 0 and 1. (*IF* for some
reason I needed to index with a bool, I'd probably write it out
in full: "theBool ? 1 : 0". Conceptually, a bool is NOT an
integral value, regardless of what the language standard says.)
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christian Engström |
last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on
Windows XP) I get the following result:
Calling 'func(d)':
'base' copy constructor
Calling 'func(*d_handle)':
'base' copy constructor
Calling 'func(d_handle)':
|
by: trying_to_learn |
last post by:
Why do we need to explicitly call the copy constructor and the operator
= , for base class and member objects in composition?
....book says "You must explicitly call the GameBoard copy-constructor or
the default constructor is automatically called instead"
Why cant the compiler do this on its own. if we are making an object
through copr construction for an inherited class , then why not simply
call the corresponding copy constructors for...
|
by: Arne |
last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that
be a clone method?
|
by: dalu.gelu |
last post by:
Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};
|
by: subramanian |
last post by:
Consider the code fragment:
class Test {
public:
Test(const Test &temp);
...
};
....
| |
by: Jeroen |
last post by:
Hi guys,
I have a simple question. If I have a class like:
class A {
A();
~A();
A(A& a);
A(int i);
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
|
by: Visame |
last post by:
#include <iostream>
using namespace std;
class A
{
public:
A() {cout << "constructor A" << endl;}
A(A& a) {cout<<"copy A"<<endl;}
virtual void Test(){cout <<"A test" <<endl;}
};
|
by: suresh |
last post by:
Hi
Could you please tell why copy constructor is not called in the first
line in main(), as mentioned in the text books. I used g++ version
4.1.2 on debian etch
thanks
suresh
# include <iostream>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |