473,396 Members | 1,770 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,396 software developers and data experts.

copy constructor in g++

the language spec tells us that a temporary class will be created when a
function's return value is an object. And the copy constructor will be
called (if it has been defined) when the temporary object is created.
But when I compile the following code with g++, there is no copy
constructor call. Why?

#include <iostream>
using namespace std;
class myclass{
int i;
public:
myclass(){ cout << "constructor ...\n";}
myclass(const myclass &ob){ cout << "copy constructor...\n";}
~myclass(){ cout << "destructor...\n";}
};
myclass foo(){ myclass temp; cout << "calling foo...\n"; return temp}
int main(){
myclass a;
a = foo();
}

Here I copy the output of the program. Supposely, there should be a
temporary object created, but I don't see its constructing/destructing
procedure, as the output shows. Why does this happen? is it a g++
implementation specific behavior? Thanks

[dash]$g++ copyconstructor1.cpp
[dash]$ a.out
constructor ...
constructor ...
calling foo...
destructor...
destructor...
Sep 29 '05 #1
1 1740
Sean wrote:
the language spec tells us that a temporary class will be created when a
function's return value is an object. And the copy constructor will be
called (if it has been defined) when the temporary object is created.
But when I compile the following code with g++, there is no copy
constructor call. Why?

#include <iostream>
using namespace std;
class myclass{
int i;
public:
myclass(){ cout << "constructor ...\n";}
myclass(const myclass &ob){ cout << "copy constructor...\n";}
~myclass(){ cout << "destructor...\n";}
};
myclass foo(){ myclass temp; cout << "calling foo...\n"; return temp}
int main(){
myclass a;
a = foo();
}
C++ also allows the copy constructor call to be eliminated for
efficiency reasons. It allows this even if the copy constructor has some
side effect.

Here I copy the output of the program. Supposely, there should be a
temporary object created, but I don't see its constructing/destructing
procedure, as the output shows. Why does this happen? is it a g++
implementation specific behavior? Thanks


It is, although I imagine many other compilers would do the same. We had
a recent post here which showed that VC++ 7 eliminated the copy
constructor call only in the case where it was user defined. If it was
not defined then an extra call to the destructor proved that the default
copy constructor was getting called. Maybe you could try the same
experiment.

john
Sep 29 '05 #2

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
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...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
0
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...
0
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...

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.