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

Question about passing variables to a function

Hi,

Assume a function which returns an object of type 'my_class':

my_class get_class()
{
...
}

Now I have the following question:

void my_function(my_class m)
{
...
}

my_function(get_class());

Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...

A similar question is about the following code:

my_class my_function()
{
...
return get_class();
}

Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?

Thanx for any answers,

Jeroen
Mar 30 '07 #1
4 1439
On 3ÔÂ30ÈÕ, ÏÂÎç4ʱ55·Ö, Jeroen <no_m...@thanx.com>wrote:
Hi,

Assume a function which returns an object of type 'my_class':

my_class get_class()
{
...
}

Now I have the following question:

void my_function(my_class m)
{
...
}

my_function(get_class());

Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...

A similar question is about the following code:

my_class my_function()
{
...
return get_class();
}

Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?

Thanx for any answers,

Jeroen
For primitive types, most modern compilers would skip the step.You
could use the smart to ease your life.But for user-define types,you
should not rely on your compiler's possible optimization.
You could write a simple program to test your compiler's intelligence
quotient.
#include <iostream>
class Foo {
public:
Foo()
{
}
Foo(const Foo &f)
{
std::cout << "Copy constructor for Foo\n";
}
};

Foo test(Foo f)
{
return Foo();
}

int main()
{
test(Foo());
return 0;
}

Mar 30 '07 #2
On 30 Mar, 09:55, Jeroen <no_m...@thanx.comwrote:
Hi,

Assume a function which returns an object of type 'my_class':

my_class get_class()
{
...
}

Now I have the following question:

void my_function(my_class m)
{
...
}

my_function(get_class());

Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...

A similar question is about the following code:

my_class my_function()
{
...
return get_class();
}

Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?
The compiler is allowed to elide the copy in cases like this, so the
copy constructor might not be called. However, even if the copy is
elided, the copy constructor must still be accessible. If my_class
were not copyable (e.g. copy constructor declared private) then the
code would not compile, regardless of whether the compiler would elide
the copy or not.

Gavin Deane

Mar 30 '07 #3
Gavin Deane schreef:
On 30 Mar, 09:55, Jeroen <no_m...@thanx.comwrote:
>Hi,

Assume a function which returns an object of type 'my_class':

my_class get_class()
{
...
}

Now I have the following question:

void my_function(my_class m)
{
...
}

my_function(get_class());

Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...

A similar question is about the following code:

my_class my_function()
{
...
return get_class();
}

Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?

The compiler is allowed to elide the copy in cases like this, so the
copy constructor might not be called. However, even if the copy is
elided, the copy constructor must still be accessible. If my_class
were not copyable (e.g. copy constructor declared private) then the
code would not compile, regardless of whether the compiler would elide
the copy or not.

Gavin Deane
Hi Gavin,

Thanks for answering. Does your answer "The compiler is allowed to elide
the copy in cases like this" refer to both cases I described? So, also
to the first case "my_function(get_class());"?

Thanks,

Jeroen
Mar 30 '07 #4
Jeroen wrote:
Assume a function which returns an object of type 'my_class':

my_class get_class()
{
...
}

Now I have the following question:

void my_function(my_class m)
{
...
}

my_function(get_class());
The copy constructor will be called in this case
>
Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...

A similar question is about the following code:

my_class my_function()
{
...
return get_class();
}
The copy-constructor does not have to be called in this case - it
depends on compiler you using
Mar 30 '07 #5

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

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
11
by: Daniel Wilcox | last post by:
I have a question, I have been given a piece of code that apparantly compiles under Visual C++ but I cannot get to compile under g++ 3.2. I have read the FAQ and delved into the Stroustrup book...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Chad | last post by:
I get the jist of this Inheritance stuff, I think, I'm just looking to understand better... I created a MustInherit base class with a constructor that had arguments. Within the constructor...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
2
by: Ed Jay | last post by:
I have two functions, one of which is called from within the other, which is called from an HTML page. The called function is passed parameters that I'd like to pass to the argument calling the...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
10
by: mcl | last post by:
Why can I not the change the value of a variable in another class, when I have passed it via a parameter list. I am sure I am being stupid, but I thought passed objects were Read/ Write eg...
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:
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?
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...
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.