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

Copy Constructor -Query

Hi,
Given below is apiece of code which I was writing to clear my concepts
of copy constructor. I have a function f() which takes Class A object
through call by value and return the same object. Copy constructor is
called when we pass the argument by value and we return from the
function and also we initialize.

In the main function, I check the number of time the copy constructor
is called, I expect it to be 3
(1) parameter passing in f(a)
(2)return in f(a)
(3) initialization of c = f(a)

But it seems the copy constructor is called twice ..Why is that ?

static int num;
A::A(const A& A1)
{
cout<<" Inside COPY Constructor " <<++num <<endl;
}
const A f(const A z)
{
cout << "Inside Function" <<endl;
return(z);
}
int main()
{
A a(5);
A c = f(a); // c is initialized by return value of function f()
cout << num; // Shouldnt num be 3 ...it is giving 2
}

Regards
-Vivek Shah
Jul 22 '05 #1
1 1609
"Vivek Shah" <sh***@ececs.uc.edu> wrote...
Hi,
Given below is apiece of code which I was writing to clear my concepts
of copy constructor. I have a function f() which takes Class A object
through call by value and return the same object. Copy constructor is
called when we pass the argument by value and we return from the
function and also we initialize.

In the main function, I check the number of time the copy constructor
is called, I expect it to be 3
(1) parameter passing in f(a)
(2)return in f(a)
(3) initialization of c = f(a)

But it seems the copy constructor is called twice ..Why is that ?


Read about "return value optimisation" or "RVO". The compiler is
allowed to optimise the creation of a temporary away and return the
object directly in your 'c'.

Victor
Jul 22 '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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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?
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
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,...

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.