Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about references and pointers [duplicate]

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(); }
like image 921
3lbom Avatar asked Dec 16 '22 01:12

3lbom


1 Answers

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.

like image 98
juanchopanza Avatar answered Jan 11 '23 22:01

juanchopanza