0

What happens, in case of copy constructor, if I use pointer in the parameter instead of reference variable? for ex.

class MyClass
{
private: int a;
         char *str;
public:...
       ...
       MyClass(MyClass *pObj)
       {
          ....
          ....
       }
};
2
  • 1
    The usual answer to What happens if...? questions is - Write a sample program and see for yourself.. Commented Oct 13, 2011 at 8:45
  • 1
    this is not a copy constructor in the traditional sense - a copy constructor has a specific signature, MyClass(const MyClass&)... Commented Oct 13, 2011 at 8:47

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I also tried this. Means it is not constructor as because we are explicitely calling it. Is there any NULL problem can be occure because reference do not hold NULL value. and pointer hold NULL. I just want to clear this.
It is an overloaded constructor. Yes pointers can be NULL and References cannot be NULL,If a reference is NULL the program is already ill formed.And Yes,In case of the overloaded constructor which takes pointer argument,You would need to gaurd against the NULL.
1

By definition, a copy constructor is a constructor with a const reference parameter to the same class. Any other constructor definition is not a "copy constructor", so it won't be invoked by the compiler when you write something like:

MyClass x = y;

or

MyClass x( y );

Comments

0

Copy constructor is supposed to make a copy from an object and not from pointer.
Why pass by Reference and not by value? Because if you don't do so, the copy constructor will go in creation of the copy of the argument and it will be an endless loop.

9 Comments

thanks. right but what will happen because I want such a case where this situation goes wrong. I tried code by using pointer and it is compiled and executed successfully. Can you give me any example where such a condition will fail.
Your idea of calling something like MyClass(MyClass*) is a copy constructor is invalid as we already know it as parametrized constructor So teh program will compile fine, run fine, but stop saying it a copy constructor. Moreover have you tried this: MyClass *pObj = NULL; MyClass Obj(pObj); Does it runs too??
thats absolutely right..It shows the segmentation fault. Now I got the answere of my question. Thanks abhinav.
@user993009 many programmers prefer to choose references as they are faster and dont need to be checked against NULL and obviously they avoid SEGFAULT. Now I guess you are new,so i should tell you that if you are satisfied, you can select the check mark at left and can vote up. This will improve your reputation.
Can you explain your reasoning/undersanding behind references as they are faster? Because that is not correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.