473,507 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy ctor and pass-by-value

Hi,
Please help me to understand compiler behavior
concerning the following code:

class X{
* *int k;
public:
* *X(){ k=1; }
* *X( X & model ){
* * * k = model.k;
* * * model.k++;
* *}
* *void get(){ cout << "k = " << k << endl; }
};
X fun( X par ){
* * * * cout << "Inside: "; par.get();
* * * * return par;
}

int main(){
* *X inst1;
* *cout << "Before: "; inst1.get();
* *fun( inst1 );
* *cout << "After: "; inst1.get();
// * X inst2 = fun( inst1 );
* *return 0;
}

The function "fun" uses pass-by-value. According to ISO, "a function can
change the values of its non-const parameters, but these changes cannot
affect the values of the arguments except where a parameter is of a
reference type". The copy constructor does not guarantee not to change
the "model" parameter, in fact it does change it. Surprisingly, gcc v.4.1.1
(Linux Mandriva) allows this code to compile (and work). Still surprisingly
(for me), uncomenting the line with second function call using
return-by-value causes compiler to throw:
error: no matching function for call to 'X::X(X)'
note: candidates are: X::X(X&)
I thought that changing the object that is about to be destroyed is not such
violation of Rules as changing passed-by-value parameter. I can't also
understand the error message: the second function call in the code seems
the same as the first, except for not ignoring the return value.

First I thought it is a bug in a compiler, but I've also tried it with
Borland Builder 6: it does not even warn in both cases.
(Of course, changing the copy constructor and adding "const" in parameter
list solves the problem.)

Thanks for your time,
Dariusz

--
|\ Dariusz Bismor
/ | \ Instytut Automatyki Politechniki ¦l±skiej
/ | \ mailto:Da************@polsl.pl
_/__|___\ http://www.ia.polsl.gliwice.pl
~~~~~~~ \________|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Nov 29 '06 #1
5 1663
Dariusz Bismor wrote:
Hi,
Please help me to understand compiler behavior
concerning the following code:

class X{
int k;
public:
X(){ k=1; }
X( X & model ){
k = model.k;
model.k++;
}
void get(){ cout << "k = " << k << endl; }
};
X fun( X par ){
cout << "Inside: "; par.get();
return par;
}

int main(){
X inst1;
cout << "Before: "; inst1.get();
fun( inst1 );
cout << "After: "; inst1.get();
// Â* X inst2 = fun( inst1 );
return 0;
}

The function "fun" uses pass-by-value. According to ISO, "a function can
change the values of its non-const parameters, but these changes cannot
affect the values of the arguments except where a parameter is of a
reference type". The copy constructor does not guarantee not to change
the "model" parameter, in fact it does change it.
Yes, the copy constructor does. The function fun however doesn't.
Surprisingly, gcc v.4.1.1 (Linux Mandriva) allows this code to compile
(and work). Still surprisingly (for me), uncomenting the line with second
function call using return-by-value causes compiler to throw:
error: no matching function for call to 'X::X(X)'
note: candidates are: X::X(X&)
Well, you try to change the return value from fun. That's not allowed in
C++.
I thought that changing the object that is about to be destroyed is not
such violation of Rules as changing passed-by-value parameter. I can't
also understand the error message: the second function call in the code
seems the same as the first, except for not ignoring the return value.
The message is all about the return value. What happens here is that in fun,
par is copied into a temporary object of type X. Then in your function
call, that temporary is copied into inst2. However, the copy construtor
wants a reference to non-const X, and C++ forbids binding non-const
references to temporaries.

Nov 29 '06 #2

But how come this is getting compiled (after un commenting) in VS2005?

You mean to say that in this case VS2005 compiler has not followed the
standards?

Dheeraj

Rolf Magnus wrote:
Dariusz Bismor wrote:
Hi,
Please help me to understand compiler behavior
concerning the following code:

class X{
int k;
public:
X(){ k=1; }
X( X & model ){
k = model.k;
model.k++;
}
void get(){ cout << "k = " << k << endl; }
};
X fun( X par ){
cout << "Inside: "; par.get();
return par;
}

int main(){
X inst1;
cout << "Before: "; inst1.get();
fun( inst1 );
cout << "After: "; inst1.get();
// X inst2 = fun( inst1 );
return 0;
}

The function "fun" uses pass-by-value. According to ISO, "a function can
change the values of its non-const parameters, but these changes cannot
affect the values of the arguments except where a parameter is of a
reference type". The copy constructor does not guarantee not to change
the "model" parameter, in fact it does change it.

Yes, the copy constructor does. The function fun however doesn't.
Surprisingly, gcc v.4.1.1 (Linux Mandriva) allows this code to compile
(and work). Still surprisingly (for me), uncomenting the line with second
function call using return-by-value causes compiler to throw:
error: no matching function for call to 'X::X(X)'
note: candidates are: X::X(X&)

Well, you try to change the return value from fun. That's not allowed in
C++.
I thought that changing the object that is about to be destroyed is not
such violation of Rules as changing passed-by-value parameter. I can't
also understand the error message: the second function call in the code
seems the same as the first, except for not ignoring the return value.

The message is all about the return value. What happens here is that in fun,
par is copied into a temporary object of type X. Then in your function
call, that temporary is copied into inst2. However, the copy construtor
wants a reference to non-const X, and C++ forbids binding non-const
references to temporaries.
Nov 29 '06 #3
Dheeraj wrote:
>
But how come this is getting compiled (after un commenting) in VS2005?

You mean to say that in this case VS2005 compiler has not followed the
standards?

Dheeraj
Nop, not in VS, but in Borland Builder 6. But still, I can't understand
why the first function call is allowed.

Dariusz

(...)
--
|\ Dariusz Bismor
/ | \ Instytut Automatyki Politechniki ¦l±skiej
/ | \ mailto:Da************@polsl.pl
_/__|___\ http://www.ia.polsl.gliwice.pl
~~~~~~~ \________|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Nov 29 '06 #4
Dheeraj wrote:
>
But how come this is getting compiled (after un commenting) in VS2005?

You mean to say that in this case VS2005 compiler has not followed the
standards?
Yes. However, there might be some command line options to switch that
compiler into a more standard compliant mode.

Nov 29 '06 #5
Dheeraj wrote:
>
But how come this is getting compiled (after un commenting) in VS2005?
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Nov 29 '06 #6

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

Similar topics

3
4453
by: mescaline | last post by:
//Consider the simple program with inheritance, plain init for A, copy ctr for B #include <iostream> using namespace std; class Base{ public: Base(){cout << "Base, default" << endl;}...
7
2665
by: skishorev | last post by:
Hi, I know What is the copy constructor. I don't know where and why we have to use copy constructor. If You know please give to me with a situation where we have to use or with a small example....
5
1681
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. ...
3
3373
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
10
3991
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
13
2438
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
6
1549
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler...
3
2039
by: subramanian100in | last post by:
If we provide any ctor for a class the compiler does not supply the default ctor. However if we do not provide the copy ctor but provide any other ctor, the compiler still supplies the copy ctor....
2
2148
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide...
15
1796
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class Test { public: Test(int xx) : x(xx) { cout << x << endl; }
0
7223
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
7114
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
7377
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...
1
7034
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
5623
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,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
0
412
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...

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.