简体   繁体   中英

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.

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. 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.

By contrast what your code is actually doing is returning a new Guitar which is a copy of the existing instance.

By comparison returning a Guitar& would also return a reference to an existing Guitar instance, but wouldn't allow you to return 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. This works if the guitars array lives longer than you need the returned reference.

In java you can "return null" because object are passed by reference. 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.

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. 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. You can't return null when returning int , right? To be able to return NULL (or equivalent), your function should be returning a pointer Guitar* , auto_ptr or smart_ptr .

Btw. when you want to always return a value, returning Guitar in C++ is something much different than in 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...

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() .

NULL is really just a pointer to the space at 0 . So returning NULL is essentially the same as returning 0 , which is not of type 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
as you search a container of Guitar pointers, you could return null
return an empty Guitar() object if none is found

Of course you would have to change the return type accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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