Sum*_*gra 5 c++ constructor copy-constructor
假设没有编译器优化.OutputBuffer_s类型对象将被创建多少次?
#include <iostream>
#include <vector>
struct OutputBuffer_s {
int encoded[10];
};
OutputBuffer_s func() {
OutputBuffer_s s;
return s;
}
int main() {
OutputBuffer_s a = func();
}
Run Code Online (Sandbox Code Playgroud)
最初,我假设了三次.
1)当调用func()时,s将在堆栈上创建对象.
2)当func()超出范围时,它会将对象的副本返回s给main().
3)将值复制到amain()中的对象,因为func()返回的值将是临时的.
我知道我错在这里,因为我用-O0in 编译g++但是在覆盖构造函数之后我只能看到一个创建.我想知道我错在哪里和为什么.
| 归档时间: |
|
| 查看次数: |
176 次 |
| 最近记录: |