MyClass(MyClass *pObj)
Is not a Copy Constructor, it is an Overloaded Constructor.
The copy constructor takes reference to the same type as argument. Ex:
MyClass(MyClass &)
The compiler generates an copy constructor for your class implicitly if you do not provide your own version.
This copy constructor will be called when compiler needs to generate a copy of an object.
The overloaded constructor said above will only be called when you explicitly call it.
Code Sample:
#include<iostream>
class MyClass
{
private: int a;
char *str;
public:
MyClass(){}
MyClass(MyClass *pObj)
{
std::cout<<"\ninside *";
}
MyClass(MyClass &pObj)
{
std::cout<<"\ninside &";
}
void doSomething(MyClass obj)
{
std::cout<<"\ndoSomething";
}
};
int main()
{
MyClass ob,ob2;
MyClass obj2(&ob); //Explicitly invoke overloaded constructor
ob.doSomething(ob2); //Calls copy constructor to create temp copy of object
return 0;
}
Note the output is:
inside *
inside &
doSomething
MyClass(const MyClass&)...