I
Igor.Smirnov
Hi,
Why g++ in Linux does not call copy constructor at return statement?
I have detected this at Sicentific Linux 4.1 (g++ v. 3.4.3) and Red Hat
Linux 8.0 (g++ v. 3.2).
Microsoft Visual Studio C++ v. 8 at Windows XP calls them as necessary
(as should be according to Stroustrup and my previous experiense),
that is it calls the copy constructor for the object, returned by
"return".
To the contrary, in Linux the copy constructor is NOT called.
How this may happen? The local automatic object should be desrtoyed. How
its content is transferred to outside scope without either constructors,
or assignment operators (they are also not colled, as the code below
shows)?
To illustrate this consider the following simple program and its output
at both Linuxes and at Windows (which is different), see below.
Who knows the explanation of this behaviour of g++, please help me to
understand this!
Thanks
Igor
------------------------------------------------------
#include <iostream>
using std::cout;
using std:stream;
class X
{
public:
int i; // just anything
X(int fi) {i = fi;}
X( X& fx)
{
cout<<"X::X(X& fx) is called\n";
*this = fx;
}
X(const X& fx)
{
cout<<"X::X(const X& fx) is called\n";
*this = fx;
}
X& operator=(const X& fx)
{
cout<<"X:perator=(const X& fx) is called\n";
if(this != &fx) i = fx.i; return *this;
}
void print(ostream& file)
{
file<<"X:rint(): i = "<<i<<'\n';
}
};
X func(int fi)
{
// create local instance of X
X x(fi);
cout<<"func:\n";
x.print(cout);
cout<<"calling return\n";
return x;
}
int main(void)
{
X ex1(0);
cout<<"Calling func\n";
ex1 = func(1);
cout<<"func is finished\n";
ex1.print(cout);
cout<<"Calling func second time\n";
X ex2( func(2) );
cout<<"func is finished\n";
ex2.print(cout);
cout<<"Calling func third time\n";
X ex3 = func(3);
cout<<"func is finished\n";
ex3.print(cout);
}
----------------------------------------
Output at Linux & and Windows Parts which appear only at Windows:
(with empty lines added)
Calling func
func:
X:rint(): i = 1
calling return
X::X(X& fx) is called
X:perator=(const X& fx) is called
X:perator=(const X& fx) is called
func is finished
X:rint(): i = 1
Calling func second time
func:
X:rint(): i = 2
calling return
X::X(X& fx) is called
X:perator=(const X& fx) is called
func is finished
X:rint(): i = 2
Calling func third time
func:
X:rint(): i = 3
calling return
X::X(X& fx) is called
X:perator=(const X& fx) is called
func is finished
X:rint(): i = 3
--
Why g++ in Linux does not call copy constructor at return statement?
I have detected this at Sicentific Linux 4.1 (g++ v. 3.4.3) and Red Hat
Linux 8.0 (g++ v. 3.2).
Microsoft Visual Studio C++ v. 8 at Windows XP calls them as necessary
(as should be according to Stroustrup and my previous experiense),
that is it calls the copy constructor for the object, returned by
"return".
To the contrary, in Linux the copy constructor is NOT called.
How this may happen? The local automatic object should be desrtoyed. How
its content is transferred to outside scope without either constructors,
or assignment operators (they are also not colled, as the code below
shows)?
To illustrate this consider the following simple program and its output
at both Linuxes and at Windows (which is different), see below.
Who knows the explanation of this behaviour of g++, please help me to
understand this!
Thanks
Igor
------------------------------------------------------
#include <iostream>
using std::cout;
using std:stream;
class X
{
public:
int i; // just anything
X(int fi) {i = fi;}
X( X& fx)
{
cout<<"X::X(X& fx) is called\n";
*this = fx;
}
X(const X& fx)
{
cout<<"X::X(const X& fx) is called\n";
*this = fx;
}
X& operator=(const X& fx)
{
cout<<"X:perator=(const X& fx) is called\n";
if(this != &fx) i = fx.i; return *this;
}
void print(ostream& file)
{
file<<"X:rint(): i = "<<i<<'\n';
}
};
X func(int fi)
{
// create local instance of X
X x(fi);
cout<<"func:\n";
x.print(cout);
cout<<"calling return\n";
return x;
}
int main(void)
{
X ex1(0);
cout<<"Calling func\n";
ex1 = func(1);
cout<<"func is finished\n";
ex1.print(cout);
cout<<"Calling func second time\n";
X ex2( func(2) );
cout<<"func is finished\n";
ex2.print(cout);
cout<<"Calling func third time\n";
X ex3 = func(3);
cout<<"func is finished\n";
ex3.print(cout);
}
----------------------------------------
Output at Linux & and Windows Parts which appear only at Windows:
(with empty lines added)
Calling func
func:
X:rint(): i = 1
calling return
X::X(X& fx) is called
X:perator=(const X& fx) is called
X:perator=(const X& fx) is called
func is finished
X:rint(): i = 1
Calling func second time
func:
X:rint(): i = 2
calling return
X::X(X& fx) is called
X:perator=(const X& fx) is called
func is finished
X:rint(): i = 2
Calling func third time
func:
X:rint(): i = 3
calling return
X::X(X& fx) is called
X:perator=(const X& fx) is called
func is finished
X:rint(): i = 3
--