我写了一些代码S s;
...... s = {};
,期待它最终结果一样S s = {};
.但事实并非如此.以下示例再现了该问题:
#include <iostream>
struct S
{
S(): a(5) { }
S(int t): a(t) {}
S &operator=(int t) { a = t; return *this; }
S &operator=(S const &t) = default;
int a;
};
int main()
{
S s = {};
S t;
t = {};
std::cout << s.a << '\n';
std::cout << t.a << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出是:
5
0
Run Code Online (Sandbox Code Playgroud)
我的问题是:
operator=(int)
选择这里,而不是"模棱两可"或另一个?S
?我的意图是s …