V
vj
Hi! I recently came across this intresting behaviour shown by Visual
C++ 6.0 compiler regarding Copy Constructors. Please tell me that is
this the standard behaviour shown by all compilers or its limited only
to VC++.
Listing 1
==================
#include <iostream>
using namespace std;
class Class1
{
int i;
public:
Class1(int ii)
{i=ii;cout<<"Class1:arameteized Constructor called with
"<<ii<<endl;}
int geti(){return i;}
Class1(Class1 & c)
{i=c.i;cout<<"Class1::Copy Constructor called with "<<i<<endl;}
};
ostream & operator<<(ostream & os,Class1 & c)
{return os<<c.geti();}
Class1 getNext(Class1 c)
{Class1 cc(c.geti()+1);
return cc;
}
void main()
{Class1 c(0);
cout<<getNext(c);
cin.get();
}
Output
===============
Class1:arameteized Constructor called with 0
Class1::Copy Constructor called with 0
Class1:arameteized Constructor called with 1
Class1::Copy Constructor called with 1
1
Listing 2
===============
/* same class but diffrent get getNext & main functions*/
Class1 getNext(Class1 c)
{return Class1(c.geti()+1);}
void main()
{cout<<getNext(Class1(0));
cin.get();
}
output
================
Class1:arameteized Constructor called with 0
Class1:arameteized Constructor called with 1 //!!Strange No Copy
Constructor
1
It seems that the compiler is bypassing the copy constructor when
creating a temporary object. It does seems sense as there no need to
call the copy constructor as the object is already created and
gauranteed to be unmodified, So it passes the object itself to the
function.
However I am not sure that this behaviour a standard behaviour of copy
constructors?
Thanks,
VJ
C++ 6.0 compiler regarding Copy Constructors. Please tell me that is
this the standard behaviour shown by all compilers or its limited only
to VC++.
Listing 1
==================
#include <iostream>
using namespace std;
class Class1
{
int i;
public:
Class1(int ii)
{i=ii;cout<<"Class1:arameteized Constructor called with
"<<ii<<endl;}
int geti(){return i;}
Class1(Class1 & c)
{i=c.i;cout<<"Class1::Copy Constructor called with "<<i<<endl;}
};
ostream & operator<<(ostream & os,Class1 & c)
{return os<<c.geti();}
Class1 getNext(Class1 c)
{Class1 cc(c.geti()+1);
return cc;
}
void main()
{Class1 c(0);
cout<<getNext(c);
cin.get();
}
Output
===============
Class1:arameteized Constructor called with 0
Class1::Copy Constructor called with 0
Class1:arameteized Constructor called with 1
Class1::Copy Constructor called with 1
1
Listing 2
===============
/* same class but diffrent get getNext & main functions*/
Class1 getNext(Class1 c)
{return Class1(c.geti()+1);}
void main()
{cout<<getNext(Class1(0));
cin.get();
}
output
================
Class1:arameteized Constructor called with 0
Class1:arameteized Constructor called with 1 //!!Strange No Copy
Constructor
1
It seems that the compiler is bypassing the copy constructor when
creating a temporary object. It does seems sense as there no need to
call the copy constructor as the object is already created and
gauranteed to be unmodified, So it passes the object itself to the
function.
However I am not sure that this behaviour a standard behaviour of copy
constructors?
Thanks,
VJ