How does copy constructor work?

后端 未结 5 806
感动是毒
感动是毒 2021-01-17 01:12

How does a copy constructor work in c++

Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\\users\\thuan\\dropbox\\homework\\css

相关标签:
5条回答
  • 2021-01-17 01:22

    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).

    0 讨论(0)
  • 2021-01-17 01:26

    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.

    0 讨论(0)
  • 2021-01-17 01:36

    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 .

    0 讨论(0)
  • 2021-01-17 01:43

    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);
    
    0 讨论(0)
  • 2021-01-17 01:43

    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;
    }
    
    0 讨论(0)
提交回复
热议问题