给定两个具有不同构造函数的类:
#include <iostream>
struct A {
int x;
A() {};
};
struct B {
int x;
B() = default;
};
int main() {
int x = 5;
x = 7;
printf("before: %d\n", x);
new(&x) A();
printf("%d\n", x);
new(&x) B();
printf("%d\n", x);
}
输出是:
before: 7
7
0
为什么default
ctor 零初始化int x
?