怎么会发生这种情况? [英] How is it that this can happen?

查看:81
本文介绍了怎么会发生这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们是电子表格/ *然后是* / CreateObject(){} //我很困惑这是合法的。

这是什么意思?



Their is a Spreadsheet /*then a*/ CreateObject(){} //I am confused how this is legal.
What does it mean?

Spreadsheet CreateObject() { return Spreadsheet(3, 2); };
int main(){ }





我尝试了什么:



他们是电子表格/ *然后是* / CreateObject(){} //我很困惑这是合法的。

这是什么意思?





Spreadsheet CreateObject(){return Spreadsheet(3,2); };

int main(){}



What I have tried:

Their is a Spreadsheet /*then a*/ CreateObject(){} //I am confused how this is legal.
What does it mean?


Spreadsheet CreateObject() { return Spreadsheet(3, 2); };
int main(){ }

推荐答案

这是一个格式为对象函数体<的函数实现/ code>喜欢

This a function implementation in the format Object function body like
int func() { return 0; }; 

对象不是一个好主意,因为Spreadsheet对象是在函数体中分配的,而副本是在栈上分配的。



更好是使用对象指针:

It is not a good idea for objects because the Spreadsheet object is allocated in the function body and the a copy on the stack.

Better is to work with object pointers:

Spreadsheet* CreateObject() { return new Spreadsheet(3, 2); };

你不会写入函数。你需要在某处删除电子表格*。

Which you wont write into a function. And you need to delete the Spreadsheet* somewhere.


这是完全合法的(和惯用的) C ++ 代码,因为 RVO [ ^ ]通常没有性能损失。
That is perfectly legal (and idiomatic) C++ code, due to RVO[^] there is usually no performance loss.


这篇关于怎么会发生这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆