Dear what is the difference between . and -> in C++ and where we have to use these.


5 Years
Discussion Span
Last Post by rubberman

Does this link, which outlines a number of scenarios when you would want to use the specified operators help? Hope it does =D... gotta love google

Edited by pbj.codez


They are both used to access members of a structure.

The dot operator is used to access members of that structure when the structure is in the code block by value. For example, say you define the following structure in the main program:

struct {int a;
int b;
int c;
int d;
} DummyVariable;

You could access any of the members of DummyVariables by using the dot operator. For example,

DummyVariable.a = 5;

You could also pass the entire structure into a function. For example,

function(struct variablename);

HOWEVER, changes you make to members of the struct within the function won't apply outside that function. If you want changes to a struct to be applicable outside the function, you have to pass that struct into the function by reference. So the function would have to be re-defined to accept a pointer (*p), and in the main program the address would be passed in (e.g. - &struct).

Then, in the function, you would access members of the struct by using the -> notation, and any changes made to the struct within the function would take affect outside the function also.


The dot (.) operator is to access member variables/functions of an object or reference to an object. The pointer-to (->) operator is to acces member variables/functions of a POINTER to an object. Example:

class foo
    int m_bar;
    foo() : bar(0) {}

int main(void)
    foo aFoo;
    foo& aFooRef = aFoo;
    foo* aFooPtr = &aFoo;
    foo* anotherFooPtr = &aFooRef;
    cout << "aFoo == " << aFoo.m_bar
         << ", aFooRef == " << aFooRef.m_Bar
         << ", aFooPtr == " << aFooPtr->m_Bar
         << ", anotherFooPtr == " << anotherFooPtr->m_bar
         << endl;
    return 0;

So, the output would be this: aFoo == 0, aFooRef == 0, aFooPtr == 0, anotherFooPtr == 0
In C++ vernacular, a reference is a "shallow copy" - it is a reference to the original object. A pointer is just that, the a pointer to the address of the original object, or in the last example a pointer to the object the second is a reference to. They are ALL accessing the same object! Yes, this is sometimes difficult to wrap your brain around, but it is critical to successfully mastering the subject of C++ programming! Good luck, and keep up with the great questions!


So, after this, if we were to increment the value of m_bar in aFoo before we printed the output, what would the output be? You get my +12 upgrade points if you get it right! :-)

This offer only applies to irfan (the original poster)! Please don't try to answer for him...

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.