地址符号&在构造函数中的含义是什么?

时间:2019-04-15 10:36:14

标签: c++ constructor symbols

我正在查找c ++库,并看到istream类,我对带有地址符号的承包商感到困惑。带有地址符号的构造函数是什么意思?

其中一个istream构造函数是

protected: iostream& (iostream&& x);

我在cplusplus.com网站上找到了它,

c++11contructors

链接:iostream



我用类似的构造函数定义了一个带有&符号的客户类:

//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类很好。为什么我的自定义类构造函数失败?我想念什么?

2 个答案:

答案 0 :(得分:2)

cplusplus.com上的信息有时...并不可靠。 (有关此问题的讨论,请参见What's wrong with cplusplus.com?。)在CPPReference上,您可以看到move构造函数只是常规的move构造函数。

答案 1 :(得分:0)

这是http://www.cplusplus.com/reference/istream/iostream/iostream/中的错误。

如果您查看https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream,将会发现

  

受保护:basic_iostream(basic_iostream && other);

相关问题