473,569 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CPP Output Question - related to copy-constructor and return by value

Hi All,

I have a doubt in understanding the output of the following program
that i executed on my system. I was using DevC++ IDE which uses minGW
based compiler.

----------------------------------------------
#include <iostream>
using namespace std;

class Integer //: public DataType
{
private:
int x;
public:
Integer(int xx = 0);
Integer(const Integer&);
~Integer();
Integer operator+(const int i);
void operator=(const Integer& i);
};

Integer::Intege r(int xx) : x(xx)
{
cout<<"Integer( int) x is "<<x<<endl;
}
Integer::Intege r(const Integer& i) : x(i.x)
{
cout<<"Integer( Integer&)"<<end l;
}
Integer::~Integ er()
{
cout<<"~Integer ()"<<endl;
}
Integer Integer::operat or+(const int i)
{
cout<<"operator + called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer::operat or=(const Integer& i)
{
cout<<"operator = called i.x is "<<i.x<<end l;
x = i.x;
}

int main()
{
Integer i1(40);
Integer i3=i1+10; //Doubt understanding this
return 0;
}
-----------------------------------
Output:

Integer(int) x is 40
operator+ called i is 10
Integer(int) x is 50
~Integer()
~Integer()
-------------------------------------
I have a doubt in understanding the execution of operator+.

Inside operator+, xx is created and is returned by value. While
returning the copy-constructor of Integer should get invoked because
the output of operator+ is being assigned to i3 which is not yet
constructed, but the actual output observed is different.

Is this any kind of optimization being performed by the compiler. Is
there anything that i am missing.

I would appreciate if someone can explain this.

Regards
Sanjay Raghani
Oct 9 '08 #1
2 2433
sanjay wrote:
I have a doubt in understanding the output of the following program
that i executed on my system. I was using DevC++ IDE which uses minGW
based compiler.

----------------------------------------------
#include <iostream>
using namespace std;

class Integer //: public DataType
{
private:
int x;
public:
Integer(int xx = 0);
Integer(const Integer&);
~Integer();
Integer operator+(const int i);
void operator=(const Integer& i);
As a side note, operator= *usually* returns a reference to the same
object. It's not required, of course.
};

Integer::Intege r(int xx) : x(xx)
{
cout<<"Integer( int) x is "<<x<<endl;
}
Integer::Intege r(const Integer& i) : x(i.x)
{
cout<<"Integer( Integer&)"<<end l;
You misrepresent the signature of your copy c-tor. The argument is a
reference to a const integer, so I'd expect the output statement to be

cout << "Integer(Intege r const&)" << endl;

<g>
}
Integer::~Integ er()
{
cout<<"~Integer ()"<<endl;
}
Integer Integer::operat or+(const int i)
{
cout<<"operator + called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer::operat or=(const Integer& i)
{
cout<<"operator = called i.x is "<<i.x<<end l;
x = i.x;
}

int main()
{
Integer i1(40);
Integer i3=i1+10; //Doubt understanding this
return 0;
}
-----------------------------------
Output:

Integer(int) x is 40
operator+ called i is 10
Integer(int) x is 50
~Integer()
~Integer()
-------------------------------------
I have a doubt in understanding the execution of operator+.

Inside operator+, xx is created and is returned by value. While
returning the copy-constructor of Integer should get invoked because
the output of operator+ is being assigned to i3 which is not yet
constructed, but the actual output observed is different.
The compiler is *allowed* to skip creation of a temporary and construct
the result of the right-hand side expression *directly* into the object
being constructed. Essentially, since the compiler sees the code for
all your functions, it can pass the reference to the constructed object
(in your case named 'i3') to the operator+ and inside the function
construct the actual 'i3' instead of 'xx'. No temporaries, no copies.
Is this any kind of optimization being performed by the compiler. Is
there anything that i am missing.
Yes, it is, and yes, you probably are.
I would appreciate if someone can explain this.
I hope I have.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 9 '08 #2
On Oct 9, 5:59 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
sanjay wrote:
I have a doubt in understanding the output of the following program
that i executed on my system. I was using DevC++ IDE which uses minGW
based compiler.
----------------------------------------------
#include <iostream>
using namespace std;
class Integer //: public DataType
{
private:
int x;
public:
Integer(int xx = 0);
Integer(const Integer&);
~Integer();
Integer operator+(const int i);
void operator=(const Integer& i);

As a side note, operator= *usually* returns a reference to the same
object. It's not required, of course.
};
Integer::Intege r(int xx) : x(xx)
{
cout<<"Integer( int) x is "<<x<<endl;
}
Integer::Intege r(const Integer& i) : x(i.x)
{
cout<<"Integer( Integer&)"<<end l;

You misrepresent the signature of your copy c-tor. The argument is a
reference to a const integer, so I'd expect the output statement to be

cout << "Integer(Intege r const&)" << endl;

<g>
}
Integer::~Integ er()
{
cout<<"~Integer ()"<<endl;
}
Integer Integer::operat or+(const int i)
{
cout<<"operator + called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer::operat or=(const Integer& i)
{
cout<<"operator = called i.x is "<<i.x<<end l;
x = i.x;
}
int main()
{
Integer i1(40);
Integer i3=i1+10; //Doubt understanding this
return 0;
}
-----------------------------------
Output:
Integer(int) x is 40
operator+ called i is 10
Integer(int) x is 50
~Integer()
~Integer()
-------------------------------------
I have a doubt in understanding the execution of operator+.
Inside operator+, xx is created and is returned by value. While
returning the copy-constructor of Integer should get invoked because
the output of operator+ is being assigned to i3 which is not yet
constructed, but the actual output observed is different.

The compiler is *allowed* to skip creation of a temporary and construct
the result of the right-hand side expression *directly* into the object
being constructed. Essentially, since the compiler sees the code for
all your functions, it can pass the reference to the constructed object
(in your case named 'i3') to the operator+ and inside the function
construct the actual 'i3' instead of 'xx'. No temporaries, no copies.
Is this any kind of optimization being performed by the compiler. Is
there anything that i am missing.

Yes, it is, and yes, you probably are.
I would appreciate if someone can explain this.

I hope I have.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks...
Oct 9 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2098
by: Taare | last post by:
Hi, I got <xsl:output method="html" encoding="iso-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system=" http://www.w3.org/TR/html4/strict.dtd"/> in my XSLT file. This should remove all XML related code and replace with HTML valid code, but with my commandline XSLT transformer(http://xmlsoft.org/XSLT/) it outputs a xmlns on...
2
3053
by: Gwl | last post by:
I'm writing an application in which the user can change the default ouput language while the program is running. I have no problem to do it when there is only a form opened, but if I have a situation like a second form opened I don't know how I could change the otuput language in the main form and in all the background forms. Any advice? ...
6
1399
by: Peter Afonin | last post by:
Hello, When I build an ASP.NET application, for some weird reason Visual Studio.Net started to put my DLLs into the VSWebCache folder under Documents and Settings instead of usual bin folder under the project directory. When I go to the project's properties and try to change the path to the output folder back to where it always was, I get...
5
3047
by: Verane | last post by:
Hi, I have read the thread named "Could not copy temporary files to the output directory" on this newsgroup. And I have the same symptoms on my machine. But I didn't find any solution suitable for me. (I don't use ASP.NET or any web service at all, only windows forms). Here is how I work : I have a solution with 10 projects inside. Each of...
2
2252
by: njr | last post by:
Having copied a number of folders from my development PC (XP) to a development server (W2003) and installed them under wwwroot (and created applications in IIS) I have problems when I include the corresponding asp.net projects in a solution. They build fine but when attempting debug (via F5) I get "Load of property 'Output Path' failed. The...
6
1339
by: Jerome Lyles | last post by:
I have a small training database: sql_tutorial. It works fine but the spacing between the output lines is too much. This is the way it looks when I copy and paste from the Konsole to this email: sql_tutorial=> SELECT prod_name FROM Products; prod_name...
2
2611
by: blabla120 | last post by:
Hello XSLT-freaks, I have a source xmlfile that I want to transform to 2 different xmlfiles. source.xml: <books> <book> <title>Die Brandmauer</title>
1
2630
by: David | last post by:
Hi, I have an MS Access app which lets me create a new product by copying data from another selected product. All works fine, except one of the copy updates which I cannot fathom. The code creates a new product and copies over the parent data from the other product first. Then related data from other tables is copied accross using the...
11
2044
by: Hello | last post by:
Hi every one. The famous Hello code below written by C++. through its mother program "Visual studio 2005" When I make copy to all files of this program and click on .EXE file away from the mother program, the output disappears immediately. Can any person add a line (may related to" Keypress", so that the output can stay without...
1
1011
by: Paul Hemans | last post by:
VS2005... I need to persist a database between builds, while in development. The problem is that the mdf file is being deleted whenever I start the application from VS. There is a setting on the mdf file "Copy to output directory" which is currently set to "Do not copy". However, that doesn't seem to be related as whenever I do a "Build...
0
7703
marktang
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...
0
7926
Oralloy
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. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6287
agi2029
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...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
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...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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
0
946
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.