1
I have this snippet of the code

Account& Company::findAccount(int id){
        for(list<Account>::const_iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i){
            if(i->nID == id){
                return *i;
            }
        }
        return 0;
    }

Is this right way to return 0 if I didn't find appropriate account? cause I receive an error:

no match for 'operator!' in '!((Company*)this)->Company::findAccount(id)'

I use it this way:

if(!(findAccount(id))){
            throw "hey";
          }

thanks in advance

helloWorld
  • 2,929
  • 7
  • 24
  • 19
  • Check http://stackoverflow.com/questions/2894891/this-code-appears-to-achieve-the-return-of-a-null-reference-in-c/2896330#2896330 for examples on how to return references – default Jun 17 '10 at 11:12
  • Did you mean to write `list::const_iterator...`, because I don't think that i->nID will work with an `Account`.. – default Jun 17 '10 at 11:18

5 Answers5

4

There is no such thing as a null reference. The standard says:

A reference shall be initialized to refer to a valid object or function. [ Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
2

You cannot return a "null reference" in C++, because there is no such concept in this language.

If either an Account or no Account can be the result, one way to do it is to return an Account *.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
1

No, this is not possible. It looks like the function may have been changed from returning a pointer (Account*) to returning a reference (Account&).

You cannot return 0/null as a reference, you will have to change the design of the function. Maybe reverting to Account* as return type.

If you only use it in an if-statement like that, you could change it to returning a std::bool. But the current signature suggests other usage patterns.

H H
  • 263,252
  • 30
  • 330
  • 514
  • why cannot return 0/null as a ref? I tried to pass a null pointer into a function as ref, it seems ok. – Alcott Oct 07 '11 at 02:43
1

Another way (except for Account*) would be to return special Account::empty object, and/or test using account.IsEmpty().

queen3
  • 15,333
  • 8
  • 64
  • 119
1

Why don't you use a standard algorithm like std::find_if.

Edit: Explanation of find_if.

Here is the reference for find_if algorithm. http://cplusplus.com/reference/algorithm/find_if/

bool isEqual(int id1, int id2) {
   return id1 == id2;
}

void foo() {
    std::list<Account> accountList;
    // Fill this list here.

    list<Account>::iterator it = std::find_if(accountList.begin(), accountList.end(), bind(isEqual, idYouSearch, _1));

    Account ac = *it;
}

You might need additional includes for bind and _1 placeholder. Propably there is a std Predicate for isEqual and you could use this instead.

Egeria
  • 26
  • 3
  • can You explain it plese, I thought that list doesn't have such algorithm – helloWorld Jun 17 '10 at 10:03
  • the algorithms don't belong to the containers. All standard containers have iterators which you can use with the standard algorithms. – Egeria Jun 17 '10 at 10:59