4

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
What are the distinctions between the various symbols (*,&, etc) combined with parameters?

I'm having a bit of trouble getting my head around this example code pasted below. Specifically, the function Buf& operator=( const Buf & );. From what I understand, this function expects an address of the object of class Buf to be returned. Two questions arise:

  1. Is it equally applicable to declare this as Buf* operator=( const Buf* );, since Buf* is also an address of an instance of class Buf? If not, why not? If so, is it just a preference of coding sytle? Which is preferable?

  2. In the corresponding function definition, *this is returned. From what I understand, this is a pointer, or address corresponding to the object of class Buf. So *this is what the pointer points to, i.e. an object of class Buf. Is this in conflict with returning Buf*? Should the function return this instead of *this ?

I guess I'm having one of those days today... please somebody help!!

using namespace std;

class Buf  { public:
    Buf( char* szBuffer, size_t sizeOfBuffer );
    Buf& operator=( const Buf & );
    void Display() { cout << buffer << endl; }

private:
    char*   buffer;
    size_t  sizeOfBuffer; };

Buf::Buf( char* szBuffer, size_t sizeOfBuffer ) {
    sizeOfBuffer++; // account for a NULL terminator

    buffer = new char[ sizeOfBuffer ];
    if (buffer)
    {
        strcpy_s( buffer, sizeOfBuffer, szBuffer );
        sizeOfBuffer = sizeOfBuffer;
    } }

Buf& Buf::operator=( const Buf &otherbuf )  {
    if( &otherbuf != this ) 
    {
        if (buffer)
            delete [] buffer;

        sizeOfBuffer =  strlen( otherbuf.buffer ) + 1; 
        buffer = new char[sizeOfBuffer];
        strcpy_s( buffer, sizeOfBuffer, otherbuf.buffer );
    }
    return *this; }

int main() {
    Buf myBuf( "my buffer", 10 );
    Buf yourBuf( "your buffer", 12 );

    // Display 'my buffer'
    myBuf.Display();

    // assignment opperator
    myBuf = yourBuf;

    // Display 'your buffer'
    myBuf.Display(); }
Community
  • 1
  • 1
3lbom
  • 139
  • 1
  • 1
  • 11

5 Answers5

4

This:

Buf& operator=( const Buf & );

returns a reference to a Buf object, not an address of a Buf object. So when the method returns *this, the caller gets a reference to the object.

This: Buf* operator=( const Buf * );

returns a pointer to a Buf, so the corresponding function would indeed return this.

Note that here:

Buf& b = <some code returning Buf&>;

b is a reference to Buf, not an address. On the other hand,

Buf c = ...
Buf* pBuf = &c;

&c in the code above is the address of c and can be used to initialize pBuf, which is a pointer to Buf. So the * and & can have different meanings depending on the context.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

From what I understand, this function expects an address of the object of class Buf to be returned.

No.

A reference to a Buf object is returned, not an address. A reference is not an address. It is an alias - something used to refer to that precise object. A reference is different from a pointer.

I suggest that you google for C++ references and pointers, or even better, that you read a good C++ book which explains this concept.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 1
    thanks. i did google it but i agree that a good book is really what i need. off to the library! – 3lbom Apr 11 '12 at 13:57
3

Buf& is not an "address", it's a reference. Syntactically, you simply return an object, and the caller gets access to the original. In the above case, return *this; returns the current object.

(Note that it is illegal to return automatic objects by reference, because they won't exist anymore when the caller tries to access them later.)

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

Behind the scenes the reference is referred as a pointer to an object. When you return *this as reference you actually return the exact object (without any copying) to the reference receiving it. The main difference between a reference and a pointer is that you can manipulate pointers (like changing the address they point to) but references cannot.

example :

int val = 4;
int *p = &val;
int &r = val;

p++ // Will pointer to the next memory located to val (addr(val) + sizeof(int)) 
(*p)++ // Will NOT increment val anymore as p pointer to another location
r++ // will increment value of val (you cannot increment the location r is pointing to). This is same as (*p)++.

both p and r are referring to the same location in memory (address of val) but the only difference is the way you access the value of val. (*p vs r)

giorashc
  • 13,691
  • 3
  • 35
  • 71
1

When you get acquianted with a difference between reference notion (SomeType&) and an address taking operator (&SomeObject), you may look into C++ FAQ, where there is a good explanation of what a reference is and how should you use it.

Steed
  • 1,292
  • 1
  • 14
  • 33