How does copy constructor work?

偶尔善良 提交于 2019-12-01 14:37:32

After you declare

InSet object1;

the object named object1 exists (created via the default constructor). Copy constructor (just like a regular constructor) creates a new object. Since the object already exists, the expression object1(object2); cannot be a copy-constructor call. For that, you need to declare your variables like this:

InSet object2(9);
InSet object1(object2);

If you wanted to copy object2 to the already existing object1, you will need an assignment operator (InSet& InSet::operator=(const InSet& other);)

Note: The compiler error is telling you that the expression (object1(object2); is a function call expression: you will need to define the function call operator (void InSet::operator()(const InSet& obj)) to make it compile (the return type could be anything else, not just void, depending on your need.) If you define the function call operator, you turn your object into what is called a functor

The compiler error (term does not evaluate to a function taking 1 arguments) is correct, but misleading wrt. to your problem (but there is no way for the compiler to know you wanted to use the copy constructor, instead of a function call)

Note: On many compilers the expression

InSet object2(9);
InSet object1 = object2;

also results in a call to the copy constructor (instead of default-constructing the object and then calling assignment operator on it), if available -- this is a possible compiler optimization.

Note: The declarations InSet object1; and InSet object2(9); are invalid if the only (regular) constructor you have is the one you listed, unless you have default values for the (regular) constructor's parameters in the class definition (where the constructor is declared), which you did not include.

You are defining the object as a variable and then you are using it like a function. You are trying to construct the object when it's already constructed

Try

IntSet object2(9);
IntSet object1(object2);

You've already constructed object1 with the default constructor when you do

IntSet object1;

To copy consrtuct you need to change this to

IntSet object1( object2 );

at some point after defining object2 (you will probably want to swap the names of your two variables).

it might help you to understand copy constructor

A copy constructor is used to declare and intilized an object from another obkect. whenever we have statement like demo d2-d1 (assume demo is class name and d1 is an already declared object of demo class),they make call to copy constructor defined in the class

for a class demo copy constructor is written as

demo(demo & d)
 {
  //copy constructor code;
 }

example:

#include<iostream.h>
class demo
{
 int data;
public:
   demo(int x)
    {
      data=x;
    }

    demo(demo & d)
     {
       data=d.data;
        cout<<"copy constructor is called";
      }
    void show()
      {
        cout<<"data ="<<data<<endl;
       }
};

void main()
{ 
 demo d1(200);
 demo d2=d1;   //copy constructor is called and object passed as a reference not value 
 d2.show;
}

as the argument of copy constructor is a constant object and in code while calling it you are passing non constant object which is not permissible .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!