问题描述
以下最少的代码说明了我的困惑。应该在函数 a(x)
中销毁 create()
吗?为什么 m
和 a
完全一样。为什么从不调用复制构造函数?
#include <iostream>
using namespace std;
class Board{
public:
int a[10000];
int s = sizeof(a)/sizeof(a[0]);
Board(int x){
for(size_t i=0; i < s; ++i){
a[i] = x;
}
};
Board(const Board& other){
memcpy(a,other.a,sizeof(int)*s);
cout << "copy constructor called" << endl;
};
Board& operator=(const Board&) = default;
~Board(){
cout << "board destroyed" << endl;
};
};
Board create(int x){
Board a(x);
cout << &a << endl;
return a;
}
Board mycreate(int x){
return create(x);
}
int main(){
auto m=create(3);
cout << &m << endl;
auto m2(mycreate(3));
cout << &m2 << endl;
}
输出:
0x7ffee7f18cf8
0x7ffee7f18cf8
0x7ffee7f0f0a8
0x7ffee7f0f0a8
board destroyed
board destroyed
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)