473,378 Members | 1,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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::Integer(int xx) : x(xx)
{
cout<<"Integer(int) x is "<<x<<endl;
}
Integer::Integer(const Integer& i) : x(i.x)
{
cout<<"Integer(Integer&)"<<endl;
}
Integer::~Integer()
{
cout<<"~Integer()"<<endl;
}
Integer Integer::operator+(const int i)
{
cout<<"operator+ called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer::operator=(const Integer& i)
{
cout<<"operator= called i.x is "<<i.x<<endl;
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 2417
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::Integer(int xx) : x(xx)
{
cout<<"Integer(int) x is "<<x<<endl;
}
Integer::Integer(const Integer& i) : x(i.x)
{
cout<<"Integer(Integer&)"<<endl;
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(Integer const&)" << endl;

<g>
}
Integer::~Integer()
{
cout<<"~Integer()"<<endl;
}
Integer Integer::operator+(const int i)
{
cout<<"operator+ called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer::operator=(const Integer& i)
{
cout<<"operator= called i.x is "<<i.x<<endl;
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...@comAcast.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::Integer(int xx) : x(xx)
{
cout<<"Integer(int) x is "<<x<<endl;
}
Integer::Integer(const Integer& i) : x(i.x)
{
cout<<"Integer(Integer&)"<<endl;

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(Integer const&)" << endl;

<g>
}
Integer::~Integer()
{
cout<<"~Integer()"<<endl;
}
Integer Integer::operator+(const int i)
{
cout<<"operator+ called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer::operator=(const Integer& i)
{
cout<<"operator= called i.x is "<<i.x<<endl;
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
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...
2
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...
6
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...
5
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...
2
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...
6
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:...
2
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
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...
11
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.