【发布时间】:2017-06-09 15:52:33
【问题描述】:
#include <bits/stdc++.h>
using namespace std;
class A {
int x;
public:
class B {
public:
int y;
B(int _y) : y(_y) {}
explicit operator A() const {
return A(y);
}
};
explicit A (int _x) : x(_x) {}
explicit A (const A& o) : x(o.x) {}
};
typedef unsigned int size_type;
int main () {
return 0;
}
错误:g++ -Wall -I./ -I/home/abdelrahman/main-repo/ -o "testt" “testt.cpp”(在目录:/home/abdelrahman/Desktop)
testt.cpp:在 成员函数'A::B::operator A() const':testt.cpp:11:14:错误:否 调用‘A::A(A)’的匹配函数 返回 A(y); ^
编译失败。
【问题讨论】:
-
将构造函数移到
class B之前? -
例如看这个问题,显式复制构造函数往往不允许按值返回:stackoverflow.com/questions/4153527/…
-
仅供参考:如果您从 As 复制构造函数中删除显式,它将编译。现在看看为什么。
-
更多想法:制作复制构造函数
explicit有什么意义吗?explicit的通常原因是避免意外转换,将A转换(复制)到另一个A对我来说似乎是安全的。如果需要,现在可以使用=delete禁用复制构造。 -
不包含
。
标签: c++ compiler-errors explicit