int main()
{
X obj1;
X obj2 = obj1;
}
//we are passing the object by reference.
//the obj1's data members can be changed if we pass through reference
//so we are using const keyword
//now why are we passing through reference actually?
//why cant with pass through value like this?
//X(obj1){}?
understood. thanks for ur post..
before i check on for what invokes the copy from the function, i need to know why are we not using pointers to pass the object? if we use what are the problems occur over there?
ur help is needed..
A: The copy constructor makes the copy for the copy constructor.
A copy ctor which takes a pointer argument is a bad idea for the reason given by keskiverto and probably for other reasons too.
I have verified that it works, however. A( const A* pa ):x(pa->x) {}// seems to work just fine if a valid pointer is given.
> A copy ctor which takes a pointer argument
is not a copy constructor
ยง12.8/2
A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6). [ Example: X::X(const X&) and X::X(X&,int=1) are copy constructors.