简体   繁体   中英

c++ is confused whether to use parameterized constructor or copy constructor

class sampleConstructor {
int x;

public:
//WE COULD DO OVERLOAD CONSTRUCTOR JUST LIKE IN FUNCTIONS | THEY ONLY DIFFERENTIATE IN NO. OF ARGUMENTS AND DATATYPE OF ARGUMENTS JUST LIKE IN FUNCTION
sampleConstructor () {  //THIS IS THE DEFAULT CONSTRUCTOR, THIS WILL AUTOMATICALLY INITIALIZE EVERYTHING TO 0 IF NOT EXPLICITLY STATED, THIS IS AUTOMATICALLY CREATED UNLESS EXPLICITLY STATED
    x = 0;
}
//sampleConstructor () { }; //IT COULD ALSO LOOK LIKE THIS
//IF YOU CREATED A PARAMETERIZED CONSTRUCTOR, DEFAULT CONSTRUCTOR WOULD NOT BE AUTOMATICALLY CREATED ANYMORE
sampleConstructor (int y) { //THIS IS A PARAMETERIZED CONSTRUCTOR
    x = y;
}
//COPY CONSTRUCTOR ARE PASSED BY REFERENCE AS TO AVOID INFINITE RECURSION
sampleConstructor (sampleConstructor &sampleCopy) { //THIS IS COPY CONSTRUCTOR, THIS IS AUTOMATICALLY CREATED UNLESS EXPLICITLY STATED | PURPOSE OF COPY CONSTRUCTOR IS TO COPY THE VALUE OF ANOTHER OBJECT
    x = sampleCopy.x;
}
void showData () {
    std::cout << "value of x is " << x << std::endl;
}
//IT IS NOT A GOOD PRACTICE TO CALL DESTRUCTOR EXPLICITLY
// ~sampleConstructor () { } ; //THIS IS DESTRUCTOR, IT IS AUTOMATICALLY CREATED BY THE COMPILER, IT MUST CONTAIN NO ARGUMENT, ONLY ONE DESTRUCTOR IS REQUIRED.
~sampleConstructor () {
    std::cout << "yes, sample constructor could also do this!" << std::endl;
}
};

int main () {
//EACH OBJECT CAN ONLY USE 1 TYPE OF CONSTRUCTOR
sampleConstructor obj1(50); //HERE WE USES PARAMETERIZED CONSTRUCTOR
sampleConstructor obj4 =sampleConstructor(50); //OBJECT CAN ALSO BE INITIALIZED LIKE THIS

sampleConstructor obj2(obj1); //HERE WE USES COPY CONSTRUCTOR, WE COPY THE VALUE OF obj1 INTO obj2
sampleConstructor obj3 = obj1; //COPY CONSTRUCTOR CAN ALSO BE INITIALIZED LIKE THIS
obj1.showData();
obj2.showData();
obj3.showData();
return (0);
}

obj4 is producing an error, it is confused whether to use parameterized constructor or copy constructor, but when i initialize it like this: sampleConstructor obj4(20), it perfectly works. sampleConstructor obj4 = sampleConstructor(20) is the same with sampleConstructor obj4(20), right?

The parameter of the copy constructor needs to be a const reference:

sampleConstructor(const sampleConstructor &sampleCopy)

Non-const (lvalue) references can't bind to rvalues, and sampleConstructor(50) is an rvalue.

Note that your code is valid as is starting from C++17, which requires sampleConstructor obj4 =sampleConstructor(50); to be exactly equivalent to sampleConstructor obj4(50); . Pre-C++17 the compilers were allowed to not emit a copy (or move) constructor call in this case, but it was required for the copy (or move) constructor itself to be available, even the compiler decided to not use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM