What is importance of const. pointer in copy constructor?

Questions by suryateja03

Showing Answers 1 - 8 of 8 Answers

supriya ahire

  • Jul 25th, 2006
 

        

      Hi  all,     

         when we try to  copy one object into another using copy constructer,we need to maintain the original copy of original object (which we are copying) so while passing object we make it constant and we pass it as a by reference.

  Thanks & Regards,

  Ms.Supriya Ahire.

Prashant Kumar

  • Aug 3rd, 2006
 

Because otherwise you will pass the object to copy as an argument of copy constructor as pass by value which by definition creates a copy and so on... an infinite call chain....Do you see that-Prashant

Anurag Verma

  • Aug 7th, 2006
 

Copy constructor is called when the copy of an object is made. One of the case is when a function argument is passed by value. So if the signature of copy constructor will take the argument by value it will get into an infinite loop. So a reference is passed and it is made constant to remain unchanged in the copy constructor.

  Was this answer useful?  Yes

It is not  Const. pointer in copy constructor but const Reference of object of same class type which we do pass in copy constructor.... and everyone has given pretty good answers  for two questions

1) what is importance of const. reference object of same class  type  in copy const...

2) Why do we use reference of class instead of simple pass by value.

Again one query why dont we use pass by pointer in copy constructor  so see following scenario

class ABC
{
public:
  ABC(){} /*Simple Constructor*/
  ABC(ABC * b) { }/*Copy Constructor*/
};

int main(int argc, char* argv[]){
  ABC * d1 = NULL;
  ABC    d2 = d1; /*Calling Copy constructor*/
  return 0;
}

See above example , if we take pointer type object in copy constructor  

two flaws are here 1) We can assign NULL value to simple object of that class

2) see this line  ABC    d2 = d1;

here d2 is ABC type while d1 is ABC* type means as per rule we are violating basic rules by allowing to assign simple type with pointer type. 

If there is any pointer type member variable in side ABC class means DEEP Copy scenario like int * m_iVal; then it will crash out during calling copy constructor by passing NULL object... so stop such mistake at design time we do use const reference object of same class type in copy constructor as a parameter.Hoping this will clear you.

  Was this answer useful?  Yes

ramachandra

  • Sep 13th, 2006
 

Copy constructor is useful when an object is initialized with another existing object or an object is passed by value to a function or an object is returned by value from a function. Shallow copy is nothing but copying members of a class by value so when an object is copied into another object all members will copied by value and if dynamic memory allocation is there the pointer will be copied but not the memory to which the pointer is pointing too. So the pointers in old and new pointing to same memory allocation. If the old objects is deleted, memory to pointer is also deleted. In this case when the new object trying to access the same memory crash will happen. This can be avoided with copy constructor explicit declaration. in the below code I created memory to the char pointer name again in copy constrctor and copied the value. I hope this gives you better idea.. Thanks

#include<iostream.h>
#include<string.h>

class A

{public:

char *name;

A(){

name = new char[20];

strcpy(name,"ramachandra");

}

A(const A &a)

{

name = new char[20];

strcpy(name,a.name);

}

};

void main()

{

A a;

cout<<a.name n;

A b = a;

cout<<b.name n;

}

  Was this answer useful?  Yes

Indrajit Paul

  • Jul 22nd, 2007
 

Thank you everybody for giving the right answer to the question.

However the use of the term "pointer" in the question still leaves a lingering doubt in my mind as to whether the questioner really typoed a reference as a pointer? or did he MEAN a "pointer".

Dear Ramachandra, the only thing I did not understand is how your explaination of shallow copy and deep copy helps answer the original question. If it does, it's fine.. otherwise you ( and I suspect many other answerers like you who does not read the question properly before answering) are only wasting other peoples times.

Please, please..and pretty please, do read the question before answering.

  Was this answer useful?  Yes

WinSound

  • May 31st, 2010
 

Copy constructor syntax:

A(const A&) for the class "A"

const - Indicates object passed as argument is not changed inside copy constructor so there may be 3 design possiblities

Case 1:  A(const A)
Not Valid: As it call copy constructor again and again.

Remember copy constructor will be called when function pass by value. So it will call copy constructor and when it reaches another copy constructor call again one more copy constrcutor as argument is passed by value.

Case 2: A(const A*)
Valid but looks ugly: But from calling point of view, caller should pass address.

(i.e )
A a1;
A a2(&a1); ---------- this looks ugly

case 3: A(const A&) - Hence C++ author decided to the 3rd case.

  Was this answer useful?  Yes

Krishna Kumar

  • Sep 25th, 2018
 

There is three query for the above question,
1) Why reference object passed as parameter to the copy constructor?
2) Why pointer object is not passed as parameter to the copy constructor?
2) What reference has been passed as const?
Answer 1) If user pass the object by value to copy as an argument of copy constructor which by definition creates a copy and so on and it will go on infinite call chain thats the reason object pass by reference.
Answer 2) If user pass pass object pointer as null then copy constructor can result into unusual behavior.
Answer 3) when user does not pass the parameter as const then it copy constructor can work partially like below case,
#include
using namespace std;
class ABC
{
public:
ABC() {cout<<"default constructor
";}
ABC(ABC &other)
{
cout<<"copy constructor
";
}
};
int main()
{
ABC foo;
ABC bar(foo);
}
But if user write below program where again copy constructor can be called (which is defined as without const reference) it will fail,
#include
using namespace std;
class ABC
{
public:
ABC() {cout<<"default constructor
";}
ABC(ABC &other)
{
cout<<"copy constructor
";
}
};
ABC fun()
{
    cout << "fun() Called
";
ABC t;
    return t;
}
  
int main()
{
ABC foo;
ABC bar = fun();
     return 0;
}
This is because the function fun() returns by value. So the compiler creates a temporary object which is copied to bar using copy constructor in the original program (The temporary object is passed as an argument to copy constructor). The reason for compiler error is, compiler created temporary objects cannot be bound to non-const references and the original program tries to do that.
But once you define the below program and pass the parameter as const reference to the copy constructor then it will work fine for all the scenario,

Code
  1. #include <iostream>

  2. using namespace std;

  3. class ABC

  4.     {

  5.        public:

  6.            ABC() {cout<<"default constructor

  7. ";}

  8.        ABC(const ABC &other)

  9.        {

  10.          cout<<"copy constructor

  11. ";

  12.        }

  13.     };

  14. ABC fun()

  15. {

  16.     cout << "fun() Called

  17. ";

  18. ABC t;

  19.     return t;

  20. }

  21.   

  22. int main()

  23. {

  24.         ABC foo;

  25.         ABC bar = fun();

  26.             return 0;

  27. }

  28.  

  29.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions