地址符号 & 在构造函数中的含义是什么?
What is the meaning of the address symbol & in a constructor?
我正在查找 c++ 库,并看到了 istream class,我与带有地址符号的承包商混淆了。带地址符号的构造函数是什么意思?
其中一个 istream 构造函数是。
protected: iostream& (iostream&& x);
我在网站 cplusplus.com、
上找到了它
link: iostream
我使用带有 & 符号的类似构造函数定义了一个客户 class:
//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
MyTest&(double b){}
};
int main(int argc,char* argv[]){
MyTest mt2(2.1);
cout << typeid(mt2).name() << endl;
return 0;
}
我使用下面的命令编译它:
g++ Test.cpp -o Test -std=c++11
但是,我收到了一些编译错误消息:
Test.cpp:7:11: error: expected unqualified-id before ‘float’
MyTest&(float b){}
^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
MyTest&(float b){}
^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
MyTest mt2(2.1);
我很困惑,C++ 库 istream class 没问题。为什么我的自定义 class 构造函数失败了?我错过了什么?
cplusplus.com 上的信息……有时不可靠。 (参见 What's wrong with cplusplus.com? for a discussion of this.) On CPPReference,您可以看到移动构造函数只是一个普通的移动构造函数。
这是 http://www.cplusplus.com/reference/istream/iostream/iostream/ 中的错误。
如果你查看 https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream,你会发现
protected: basic_iostream( basic_iostream&& other );
我正在查找 c++ 库,并看到了 istream class,我与带有地址符号的承包商混淆了。带地址符号的构造函数是什么意思?
其中一个 istream 构造函数是。
protected: iostream& (iostream&& x);
我在网站 cplusplus.com、
上找到了它link: iostream
我使用带有 & 符号的类似构造函数定义了一个客户 class:
//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
MyTest&(double b){}
};
int main(int argc,char* argv[]){
MyTest mt2(2.1);
cout << typeid(mt2).name() << endl;
return 0;
}
我使用下面的命令编译它:
g++ Test.cpp -o Test -std=c++11
但是,我收到了一些编译错误消息:
Test.cpp:7:11: error: expected unqualified-id before ‘float’
MyTest&(float b){}
^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
MyTest&(float b){}
^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
MyTest mt2(2.1);
我很困惑,C++ 库 istream class 没问题。为什么我的自定义 class 构造函数失败了?我错过了什么?
cplusplus.com 上的信息……有时不可靠。 (参见 What's wrong with cplusplus.com? for a discussion of this.) On CPPReference,您可以看到移动构造函数只是一个普通的移动构造函数。
这是 http://www.cplusplus.com/reference/istream/iostream/iostream/ 中的错误。
如果你查看 https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream,你会发现
protected: basic_iostream( basic_iostream&& other );