何时在 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.
因此我有两个选择:
- 返回一个指针
- 返回一个标量
返回一个指针看起来像这样:
Returning a pointer would look like this:
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;
}
这非常简单,这正是我在这种情况下想要的:感觉就像一个引用,它不能为空.如果对象超出客户端代码的范围,则将其删除.很方便.但是,我很少看到有人这样做,这有什么原因吗?如果我返回标量而不是指针或引用,是否存在某种性能问题?
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.
相关文章