简体   繁体   English

从返回C ++对象的函数返回null

[英]Returning null from function which returns a C++ object

I'm doing an example from Head First Object Oriented Analysis and Design, but I'm coding in C++ and I don't know how to write the equivalent of "return null" from java. 我在Head First面向对象分析和设计方面做了一个例子,但是我用C ++编写代码,我不知道如何从java编写等效的“return null”。

Guitar getGuitar(string serialNumber)
{
    for (vector<Guitar*>::iterator it = guitars.begin(); it != guitars.end(); ++it)
    {
        if ((*it)->getSerialNumber() == serialNumber)
        {
            return **it;
        }
    }
    //return null ( java); What should return here ?!
}

Java references are like C++ pointers. Java引用就像C ++指针。 So if you're returning a reference to an existing Guitar instance or null, then making your function return a Guitar* is probably what you want. 因此,如果您要返回对现有Guitar实例的引用或null,那么使您的函数返回Guitar *可能就是您想要的。

By contrast what your code is actually doing is returning a new Guitar which is a copy of the existing instance. 相比之下,您的代码实际上正在做的是返回一个新的Guitar,它是现有实例的副本。

By comparison returning a Guitar& would also return a reference to an existing Guitar instance, but wouldn't allow you to return null. 相比之下,返回一个Guitar并且还会返回对现有Guitar实例的引用,但不允许返回null。

And obviously when returning a Guitar* the caller should know that it's part of the contract that this is an existing instance. 显然,当返回吉他*时,调用者应该知道这是合同的一部分,这是一个现有的实例。 Sometimes you want to write a function that allocates a new instance on the stack. 有时您想编写一个在堆栈上分配新实例的函数。 Sometimes you want some advanced reference counting or garbage collection system. 有时您需要一些高级引用计数或垃圾收集系统。

You should return a Guitar* instead. 你应该返回Guitar* This works if the guitars array lives longer than you need the returned reference. 如果guitars阵列的寿命比您需要的返回引用长,则此方法有效。

In java you can "return null" because object are passed by reference. 在java中,您可以“返回null”,因为对象是通过引用传递的。 But in your example, your function return an object by value. 但在您的示例中,您的函数按值返回一个对象。 That's it, you have to return something of type "Guitar", and NULL is not of type Guitar. 就是这样,你必须返回类型为“Guitar”的东西,而NULL不是Guitar类型。

Instead of doing it the Java way, you should do it the C++ way which is throwing an exception in the case where you don't find any item. 而不是以Java方式进行,你应该采用C ++方式,在没有找到任何项目的情况下抛出异常。 But by the way, it depends on whether the item you are looking for can be missing or not. 但顺便说一下,这取决于你要找的物品是否可以丢失。

In C++ object is more like a struct, or value. 在C ++中,对象更像是结构或值。 You can't return null when returning int , right? 返回int时不能返回null,对吗? To be able to return NULL (or equivalent), your function should be returning a pointer Guitar* , auto_ptr or smart_ptr . 为了能够返回NULL (或等效),您的函数应该返回指针Guitar*auto_ptrsmart_ptr

Btw. 顺便说一句。 when you want to always return a value, returning Guitar in C++ is something much different than in Java. 当你想要总是返回一个值时,在C ++中返回Guitar就像在Java中一样。 There may be copy constructor involved (or there may be not -- depends) and if you return a subclass of Guitar , you'll definitely run into troubles... 可能有复制构造函数(或者可能没有 - 依赖),如果你返回一个Guitar的子类,你肯定会遇到麻烦......

If you're searching through a container like this, then you should return an iterator, not an object, and let the caller dereference it. 如果你正在搜索这样的容器,那么你应该返回一个迭代器,而不是一个对象,让调用者取消引用它。 And if the guitar is not found, you return guitars.end() . 如果找不到吉他,则返回guitars.end()

NULL is really just a pointer to the space at 0 . NULL实际上只是指向0空格的指针。 So returning NULL is essentially the same as returning 0 , which is not of type Guitar . 因此返回NULL基本上与返回0相同,而不是类型Guitar

You can do it if you make your function return a pointer to a Guitar, but returning a pointer is a lot different from returning a value. 如果使函数返回指向吉他的指针,则可以执行此操作,但返回指针与返回值有很大不同。

You have several options, depending on what the program does 您有多种选择,具体取决于程序的功能

return the end() iterator, to signal that nothing was found 返回end()迭代器,表示没有找到任何内容
as you search a container of Guitar pointers, you could return null 当你搜索一个吉他指针的容器时,你可以返回null
return an empty Guitar() object if none is found 如果没有找到,则返回一个空的Guitar()对象

Of course you would have to change the return type accordingly. 当然,您必须相应地更改返回类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM