-
June 30th, 2009, 11:30 AM
#46
Re: class constructor question
Here is the code that I worked with:
Code:
#include <iostream>
using namespace std;
class test
{
int c;
friend void funkcija(test);
public:
int d;
test(){ cout<<"Unparametrized constructor."<<endl;}
test(const test &objekat)
{
cout<<"Copy constructor called."<<endl;
}
};
void funkcija(test objekat){
cout<<objekat.c<<endl;
cout<<objekat.d;
}
int main()
{
test ob;
funkcija(ob);
system("PAUSE");
return 0;
}
The copy constructor is made to make deep copy of the object. So to make sure that the members of the class test (c,d) exist I printed their values in the function (no matter the printed text is non-understandable, I can still change their values (for ex. object.c=5 or object.d=33) so they obviously exist.
-
June 30th, 2009, 11:48 AM
#47
Re: class constructor question
Originally Posted by StGuru
So to make sure that the members of the class test (c,d) exist I printed their values in the function (no matter the printed text is non-understandable, I can still change their values (for ex. object.c=5 or object.d=33) so they obviously exist.
Then show an example where you actually initialise the member variable (only d, since you cannot initialise c from the main function), then in foo the member variable is correctly copied despite the lack of a correct implementation of the copy constructor.
Looking at the lack of initialisation, my guess is that you are mistaken: the member variables are not being copied, but you fooled yourself into believing they were copied.
-
July 1st, 2009, 02:43 AM
#48
Re: class constructor question
Originally Posted by laserlight
Then show an example where you actually initialise the member variable (only d, since you cannot initialise c from the main function), then in foo the member variable is correctly copied despite the lack of a correct implementation of the copy constructor.
Looking at the lack of initialisation, my guess is that you are mistaken: the member variables are not being copied, but you fooled yourself into believing they were copied.
Actually, you're right. I misunderstood the terms 'copy' and 'create' :-).
I got an interesting problem.
Code:
test func(test object)
{
return object;
}
test ob=func(ob1);
We got several callings of the copy constructor.
I - object is passed by value as a parameter to a function
II - object is returned from a function
III- object is created from another object of the same type
But why the copy constructor of test ob=func(ob1); is called first, where there is nothing to copy?
-
July 1st, 2009, 06:37 AM
#49
Re: class constructor question
Originally Posted by StGuru
But why the copy constructor of test ob=func(ob1); is called first, where there is nothing to copy?
What do you mean? Once again, I suggest that you post a small and simple compilable program that demonstrates what you are talking about. Post the output you get as well, keeping in mind that the compiler may elide copy construction in some cases.
-
July 1st, 2009, 10:50 AM
#50
Re: class constructor question
Thanks for the suggestions. Here is the code:
Code:
#include <iostream>
using namespace std;
class test
{
int c;
friend test funkcija(test);
public:
test(){ cout<<"Non-parametric constructor called."<<endl;}
test(const test &objekat)
{
cout<<"Copy constructor called."<<endl;
}
int d;
};
test funkcija(test objekat)
{ return objekat; }
int main()
{
test ob;
ob.d=5;
test ob1=funkcija(ob);
system("PAUSE");
return 0;
}
-
July 1st, 2009, 11:29 AM
#51
Re: class constructor question
Okay, so what is the expected and actual output?
-
July 1st, 2009, 02:31 PM
#52
Re: class constructor question
I expect the copy constructor to be called three times, but its called 2 times. Why?
-
July 2nd, 2009, 12:07 AM
#53
Re: class constructor question
Originally Posted by StGuru
I expect the copy constructor to be called three times, but its called 2 times. Why?
Because one of those copy constructor invocations that you expected was elided by the compiler. (In particular, there is no need to create a temporary to be copied to the object to be initialised since the object to be initialised can be initialised as a copy of the local variable.)
-
July 2nd, 2009, 02:55 AM
#54
Re: class constructor question
Originally Posted by StGuru
I expect the copy constructor to be called three times, but its called 2 times. Why?
Search for 'return value optimization'
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
July 2nd, 2009, 04:04 AM
#55
Re: class constructor question
Originally Posted by StGuru
I expect the copy constructor to be called three times, but its called 2 times. Why?
Go back to my post #21:
Also note that copy construction can be removed by the compiler if it is detected that the copy is superfluous
In other words, you can't predict how many times a copy constructor or assignment operator is called when given code that can generate multiple calls to these functions.
That's why when you write a user-defined assignment operator or copy constructor, you can't write it with the expectation it will be called a certain number of times. These operations could be called once, twice, three times, ten times, or no times, given the same piece of code and compiled using different compilers and/or compiler options.
Regards,
Paul McKenzie
-
July 2nd, 2009, 08:15 AM
#56
Re: class constructor question
Does it mean that the compiler does not create copy for the returning object since it already been copied (before passed by value as a parameter to a function and copy constructor called)?
-
July 2nd, 2009, 08:52 AM
#57
Re: class constructor question
Does it mean that the compiler does not create copy....
You've got the general idea. As a theme you can expect that modern compiler optimizations take every effort to fashion results which "mean" exactly the same thing as what you've written, but with as few steps as possible, given the level of research. Compilers have improved dramatically over the years.
Even when optimizations are turned off, some of the very obvious ones are still used, like genuinely superfluous copies.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|