C++ `A=A()`有效吗?下面调用了哪些运算符/方法?

C++ `A=A()`有效吗?下面调用了哪些运算符/方法?,c++,c++11,constructor,language-lawyer,copy-constructor,C++,C++11,Constructor,Language Lawyer,Copy Constructor,给定以下代码: #include <iostream> class A { public: int x; public: A() : x(0) { std::cout << "ctor" << std::endl; } A(const A& o) : x(o.x) { std::cout << "copy ctor" << std::endl; } A& operator=(const A&

给定以下代码:

#include <iostream>

class A {
 public:
  int x;

 public:
  A() : x(0) { std::cout << "ctor" << std::endl; }
  A(const A& o) : x(o.x) { std::cout << "copy ctor" << std::endl; }
  A& operator=(const A& o) { x = o.x; std::cout << "copy asgnmt" << std::endl; return *this; }
};

int main() {
  A a = A();
  return 0;
}
和输出:

ctor
Is
A=A()?或者这仅仅是编译器的依赖?如果代码符合标准,那么下面调用哪些方法
A()
应该什么也不返回,不是吗?

A()
执行,它创建了一个无名的临时对象


A=A()为,
a
从上述临时值初始化。从输出中可以看到,默认构造函数
A
用于直接在此处初始化
A
,因为

有趣的问题。尝试定义移动语义。它可能在引擎盖下使用移动分配操作符。
A A=A()--为什么您认为这是无效的?@duong_dajgja构建了一个对象。无效的是
a
的赋值运算符中缺少
返回值。打开编译器警告。<代码>()>代码>是一个默认的构造函数调用。@ -Duang-Dajgja——考虑-<代码> Boo-Fo(const a & x){true;} < /Cord>,然后<代码> int(){Auto B= FoO(A-());}< /Case>——所以<代码> A()> <代码>不“返回任何东西”。
ctor