没有新的C ++对象 [英] C++ Object without new

查看:54
本文介绍了没有新的C ++对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,但是多年来我都没有正确地完成c ++的工作,所以我对此感到困惑.另外,在互联网上查找(而不是尝试)不是最简单的事情(至少对我而言).

this is a really simple question but I havn't done c++ properly for years and so I'm a little baffled by this. Also, it's not the easiest thing (for me at least) to look up on the internet, not for trying.

为什么不使用new关键字,它如何工作?

Why doesn't this use the new keyword and how does it work?

基本上,这是怎么回事?

Basically, what's going on here?

CPlayer newPlayer = CPlayer(position, attacker);

推荐答案

此表达式:

CPlayer(position, attacker)

使用上述构造函数创建类型为CPlayer的临时对象,然后:

creates a temporary object of type CPlayer using the above constructor, then:

CPlayer newPlayer =...;

使用复制构造函数将提到的临时对象复制到newPlayer.更好的方法是编写以下代码以避免出现临时事件:

The mentioned temporary object gets copied using the copy constructor to newPlayer. A better way is to write the following to avoid temporaries:

CPlayer newPlayer(position, attacker);

这篇关于没有新的C ++对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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