Re: map problem, I'm not a student

From: Matthias Käppler (nospam_at_digitalraid.com)
Date: 12/07/04


Date: Tue, 07 Dec 2004 21:23:41 +0100

First of all, when looking at your code, I have the impression you don't
understand what a reference is in C++. You declare your function returning
'V' references, however, on failure you return NULL.

NULL in C++ is *not* the "empty" reference. In fact in C++ there is no such
thing as a "NULL-reference". References ain't pointers. Don't confuise them
with the way Java references are handled. In C++, a reference is merely an
alias for another object. It's basically like having two names to access
the same object.
So unless you want to return references to pointers, which is certainly a
dubious practice, you're in trouble.
OTOT, Since NULL is usually defined as 0 or (void*)0, I don't even try to
imagine what will happen when you instantiate your template with an invalid
type.

Billy Patton wrote:
> Not to the next problem. I have:
> V& Value(K& v)
> {
> std::map<K,V>::iterator iter = this->find(v); <<< LINE# 23
> if( iter != this->end() )
> return iter->second;
> return NULL;
> }
>
> THe compiler complains about :
> x.cxx: In member function `V& HashTable<K, V>::Value(K&)':
> x.cxx:23: error: expected `;' before "iter"
> x.cxx:24: error: `iter' undeclared (first use this function)
> x.cxx:24: error: (Each undeclared identifier is reported only once for
> each function it appears in.)



Relevant Pages

  • Re: =& when creating new object
    ... $reference point to the same memory location. ... Please read the chapter called "references are not pointers" in the ... should not consider it an address in PHP. ...
    (comp.lang.php)
  • Re: Delphi Classes/Pointers] Nil references causing problems.
    ... In the context of this group I often write "Pascal" instead of Delphi or OPL, ... references - something like C++ references, ... Almost every language allows to express things in a specific manner, ... occupation I'm tired of seeing pointers all around, and the many tests for NULL ...
    (comp.lang.pascal.delphi.misc)
  • Re: =& when creating new object
    ... & means 'a reference to the memory location where the value is stored'. ... That was probably the single most unique new concept (pointers and address-of) I had conquer when I learned C, coming from a Fortran background as I did. ... Please read the chapter called "references are not pointers" in the manual. ... But you should not consider it an address in PHP. ...
    (comp.lang.php)
  • Re: PHP5 and class inheritance question
    ... describe references in relationship to something which doesn't exist. ... knowledge of pointers as defined by another language. ... You're the one who tried to compare PHP references to something which ... get a clue jerry. ...
    (comp.lang.php)
  • Re: Deleting objects obtained from a STL library
    ... >> I want to use an STL libarary to hold a bunch of objects I create. ... >> besides using pointers rather than references for the STL library? ... The container uses the copy constructor, if no copy c_tor is provided ...
    (comp.lang.cpp)