简体   繁体   English

c++ 混淆是使用参数化构造函数还是复制构造函数

[英]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. obj4 产生错误,是否使用参数化构造函数或复制构造函数感到困惑,但是当我像这样初始化它时:sampleConstructor obj4(20),它完美地工作。 sampleConstructor obj4 = sampleConstructor(20) is the same with sampleConstructor obj4(20), right? sampleConstructor obj4 = sampleConstructor(20) 和 sampleConstructor obj4(20) 一样吧?

The parameter of the copy constructor needs to be a const reference:复制构造函数的参数需要是一个 const 引用:

sampleConstructor(const sampleConstructor &sampleCopy)

Non-const (lvalue) references can't bind to rvalues, and sampleConstructor(50) is an rvalue.非常量(左值)引用不能绑定到右值,而sampleConstructor(50)是一个右值。

Note that your code is valid as is starting from C++17, which requires sampleConstructor obj4 =sampleConstructor(50);请注意,您的代码从 C++17 开始有效,这需要sampleConstructor obj4 =sampleConstructor(50); to be exactly equivalent to sampleConstructor obj4(50);完全等同于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.在这种情况下,C++17 之前的编译器被允许不发出复制(或移动)构造函数调用,但复制(或移动)构造函数本身必须可用,即使编译器决定不使用它.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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