3

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 ?!
}
3
  • 2
    You cannot return null unless you return a Guitar* Commented Mar 19, 2011 at 23:26
  • Even if you change return type to Guitar*, return **it; does actually return an object of type Guitar. You are trying to mix both which is not correct. Commented Mar 19, 2011 at 23:51
  • 1
    It's worth noting that if you do return an iterator, what you get is nearly identical to std::find. Commented Mar 19, 2011 at 23:55

8 Answers 8

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes, i should return a pointer to Guitar, ah, i let myself fooled by the java code :) I was a little confusing, because i never encountered this before, to cannot return 0 when returning object, i didint realize it wasnt returning a pointer to object. Thanks
3

You should return a Guitar* instead. This works if the guitars array lives longer than you need the returned reference.

1 Comment

Yes, how could I miss that. Thank you.
1

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.

Comments

1

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

Comments

1

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

1 Comment

Yes maybe it could be better, but this first design is sure wrong, because next in the book it will tell me the wrong thinks in the actual code, and correct them, so maybe your solution will be in the book :)
1

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.