64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <memory>
#include <utility>
using namespace std;
class strange {
public:
mutable size_t count = 20;
strange() {
}
strange(const strange& o) :
count(o.count) {
cout << "strange(const strange& o)" << endl;
}
strange(const strange&& o) :
count(o.count) {
cout << "strange(const strange&& o)" << endl;
o.count = 0;
}
void testMethod(const strange&) {
cout << "testMethod(const &)" << endl;
}
void testMethod(const strange&&) {
cout << "testMethod(const &&)" << endl;
}
void testMethod(strange &) {
cout << "testMethod(&)" << endl;
}
void testMethod(strange &&) {
cout << "testMethod(&&)" << endl;
}
//==========================================
void testMethod() const& {
cout << "testMethod()const &" << endl;
}
void testMethod() & {
cout << "testMethod()&" << endl;
}
void testMethod() && {
cout << "testMethod()&&" << endl;
}
// OK:带来引用限定符...
void testMethod() const&& {
cout << "testMethod()const &&" << endl;
}
};
int main(int argc, char **argv) {
const strange s;
strange s2 = s;
strange s3 = move(s);
cout << s.count << endl;
cout << s2.count << endl;
cout << s3.count << endl;
s2.testMethod(move(s)); // OK...
s2.testMethod(s);// OK...
s2.testMethod(s2);// OK...
s2.testMethod(move(s2));// OK...
s2.testMethod();// OK...
s.testMethod();// OK...
strange().testMethod();// OK...
move(s).testMethod();// ERROR...
// error: call of overloaded 'testMethod()' is ambiguous
// note: candidates are:
// ..\src\Test3.cpp:40:7: note: void strange::testMethod() const &
// void testMethod() const& {
// ^
// ..\src\Test3.cpp:53:7: note: void strange::testMethod() const &&
// void testMethod() const&& {
// ^
return 0;
}
strange(const strange& o)
strange(const strange&& o)
0
20
20
testMethod(const &&)
testMethod(const &)
testMethod(&)
testMethod(&&)
testMethod()&
testMethod()const &
testMethod()&&