The Arrow Member Operator in C++

c++, pass-by-reference, pointers, reference

Solution

Good Question,

Dot(.) this operator is used for accessing the member function or sometime the data member of a class or structure using instance variable of that class/Structure.

object.function(); 
object.dataMember; //not a standard for class.

arrow(->) this operator is used for accessing the member function or sometime the data member of a class or structure but using pointer of that class/Structure.

ptr->function();
ptr->datamember; //not a standard for class.

Problem

I am quite new to using C++. I have handled Java and ActionScript before, but now I want to learn this powerful language. Since C++ grants the programmer the ability to explicitly use pointers, I am quite confused over the use of the arrow member operator. Here is a sample code I tried writing. main.cpp: ``` #include <iostream> #include "Arrow.h" using namespace std; int main() { Arrow object; Arrow *pter = &object; object.printCrap(); //Using Dot Access pter->printCrap(); //Using Arrow Member Operator return 0; } ``` Arrow.cpp ``` #include <iostream> #include "Arrow.h" using namespace std; Arrow::Arrow() { } void Arrow::printCrap(){ cout << "Steak!" << endl; } ``` In the above code, all it does is to print steak, using both methods (Dot and Arrow). In short, in writing a real practical application using C++, when do I use the arrow notation? I’m used to using the dot notation due to my previous programming experience, but the arrow is completely new to me.

Original source

Related problems