您好我试图理解在C ++中传递对象的基础知识,并且我理解在将对象传递给函数时,会调用该对象的复制构造函数,如果函数返回一个对象,则移动构造函数是叫回归主力。我也理解如果一个对象超出范围,它们会被析构函数破坏。但是我的程序中似乎有一个额外的析构函数。
输出:
Top of Program
Constructors here
Constructor
Constructor
End of Constructors
BoxSendReturn function
Copy Constructor
Inside BoxSendReturn
Move Constructor
Move Assignment
Destructor
Destructor
Destructor
End of Box Send Return
Destructor
Destructor
以下是代码:
enter code here
#include<iostream>
using namespace std;
class Box{
private:
public:
Box(){ cout<<"Constructor"<<endl; }
~Box(){ cout<<"Destructor"<<endl; }
Box (const Box &other) { cout<<"Copy Constructor"<<endl; }
Box (Box &&other) { cout<<"Move Constructor"<<endl; }
Box operator=(const Box &other){ cout<<"Copy Operator"<<endl; }
Box operator=(Box &&other){ cout<<"Move Assignment"<<endl;}
};
Box BoxSendReturn(Box b){
cout<<"Inside BoxSendReturn"<<endl;
return b;
}
int main(){
cout<<"Top of Program"<<endl;
cout<<"Constructors here"<<endl;
Box b1, b2;
cout<<"End of Constructors"<<endl;
cout<<"BoxSendReturn function"<<endl;
b1 = BoxSendReturn(b2);
cout<<"End of Box Send Return"<<endl;
return 0;
}
答案 0 :(得分:6)
您的代码具有未定义的行为:
<?php
$code = $_POST[$code];
$code = '7613';
if($code == '7613') {
echo " it worked! ";
} else {
echo "it did not work !";
}
?>
<form action="login.php" method="post">
<input type="password" name="code" />
<input type="submit" name="submit" value="Submit" />
</form>
签名的赋值运算符必须按值返回对象,但它们会错过 Box operator=(const Box &other){ cout<<"Copy Operator"<<endl; }
Box operator=(Box &&other){ cout<<"Move Assignment"<<endl;}
语句。如果你确定ctors和dtors的数量匹配:
live code
Btw:赋值运算符通常返回引用。它们没有必要,但这种方式赋值运算符具有与嵌入类型相同的行为(返回左值等),并且没有理由进行额外的复制。