什么时候返回一个指针,标量和参考在C ++? [英] When to return a pointer, scalar and reference in C++?

查看:185
本文介绍了什么时候返回一个指针,标量和参考在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Java转移到C ++,有点困惑的语言的灵活性。有一点是有三种方法来存储对象:一个指针,一个引用和一个标量(如果我正确理解,存储对象本身)。

I'm moving from Java to C++ and am a bit confused of the language's flexibility. One point is that there are three ways to store objects: A pointer, a reference and a scalar (storing the object itself if I understand it correctly).

我倾向于尽可能使用引用,因为它尽可能接近Java。在一些情况下,这是不可能的:

I tend to use references where possible, because that is as close to Java as possible. In some cases, e.g. getters for derived attributes, this is not possible:

MyType &MyClass::getSomeAttribute() {
    MyType t;
    return t;
}

这不会编译,因为 t 只存在于 getSomeAttribute()的范围内,如果我返回一个引用,它将无法在客户端使用它之前。

This does not compile, because t exists only within the scope of getSomeAttribute() and if I return a reference to it, it would point nowhere before the client can use it.

因此,我有两个选项:


  1. 返回指针

  2. 返回标量

返回指针的格式如下:

MyType *MyClass::getSomeAttribute() {
    MyType *t = new MyType;
    return t;
}



这个工作,但客户端必须检查 NULL 为了真的确定,引用不需要的东西。另一个问题是,调用者必须确保 t 被释放,我宁愿不处理,如果我可以避免它。

This'd work, but the client would have to check this pointer for NULL in order to be really sure, something that's not necessary with references. Another problem is that the caller would have to make sure that t is deallocated, I'd rather not deal with that if I can avoid it.

另一种方法是返回对象本身(标量):

The alternative would be to return the object itself (scalar):

MyType MyClass::getSomeAttribute() {
    MyType t;
    return t;
}

这很简单,只是我想在这种情况下:引用和它不能为null。如果对象超出客户端代码的范围,则会被删除。相当方便。然而,我很少看到任何人这样做,有没有理由呢?如果我返回一个标量,而不是一个指针或引用,是否有某种性能问题?

That's pretty straightforward and just what I want in this case: It feels like a reference and it can't be null. If the object is out of scope in the client's code, it is deleted. Pretty handy. However, I rarely see anyone doing that, is there a reason for that? Is there some kind of performance problem if I return a scalar instead of a pointer or reference?

处理这个问题最常见/优雅的方法是什么?

What is the most common/elegant approach to handle this problem?

推荐答案

按值返回。编译器可以优化离开副本,所以最终结果是你想要的。一个对象被创建,并返回给调用者。

Return by value. The compiler can optimize away the copy, so the end result is what you want. An object is created, and returned to the caller.

我认为你很少看到人这样做的原因是因为你看错了C ++代码。 ;)
大多数来自Java的人感觉不舒服做这样的事情,所以他们在这个地方调用 new 。然后他们得到内存泄漏的地方,必须检查NULL和所有其他问题,可以导致。 :)

I think the reason why you rarely see people do this is because you're looking at the wrong C++ code. ;) Most people coming from Java feel uncomfortable doing something like this, so they call new all over the place. And then they get memory leaks all over the place, have to check for NULL and all the other problems that can cause. :)

也许值得指出的是,C ++引用与Java引用的共同点很少。
Java中的引用更类似于指针(它可以重复,或设置为NULL)。
事实上,唯一的真正的区别是,指针可以指向一个垃圾值(如果它是未初始化的,或者它指向一个超出范围的对象),并且你可以做指针算术一个指向数组的指针。
C ++引用是对象的别名。 Java引用的行为不像这样。

It might also be worth pointing out that C++ references have very little in common with Java references. A reference in Java is much more similar to a pointer (it can be reseated, or set to NULL). In fact the only real differences are that a pointer can point to a garbage value as well (if it is uninitialized, or it points to an object that has gone out of scope), and that you can do pointer arithmetics on a pointer into an array. A C++ references is an alias for an object. A Java reference doesn't behave like that.

这篇关于什么时候返回一个指针,标量和参考在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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