There are two type of object initialisation using copy constructor:
Class object2(val1, val2); // <--- (1)
Same can be done by copying the contents of another class:
Class object1(val1, val2);
Class object2 = object1;  // <--- (2)
What is the difference between (1) and (2) ? Are they explicit calls and implicit calls or does it have to do with operator overloading?
Both constructs use constructors, but different constructors. First is a constructor taking two arguments, second is normally the copy constructor (can be defaulted). The explicit declarations should be like:
class Class {
    // constructor taking 2 args
    Class(int val1, const std::string& val2);
    // copy ctor
    Class(const Class& other);
    /* you could optionaly have a move ctor:
    Class(Class&& other); */
    ...
};
Here
Class object2(val1, val2);
will call the constructor with two arguments
Class(type a, type b);
Class object2 = object1;
will call the copy constructor
Class(const Class&);
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With