7 minute read

Introduction

The dot operator . and the arrow operator -> are both useful operators that are widely used for various things related to C++. We will explain their uses in this article.

Dot operator .

Basic usage

In C++, the dot operator (.) is used to access the members of a struct or class. It can be used to access both data members (i.e., variables) and member functions (i.e., methods) of the struct or class.

For example, if you have a class called “Person” with a data member called “name” and a member function called “setName”, you would use the dot operator to access them like so:

#include <iostream>

class Person {
public:
    void setName(std::string newName) {
        name = newName;
    }
    std::string name;
};

int main() {
    Person somePerson;
    somePerson.setName("John Smith"); 
    std::cout << "somePerson name is: "<< somePerson.name << std::endl;
    return 0;
}

Here, we create an instance of the Person class called somePerson, and use the dot operator to call the setName member function and set the name to “John Smith”. Next, we can use the dot operator again to access the name variable and print it.

Using it with pointers

Now, we can take the dot operator a step further and start using it with pointers. We recommend first reading all about pointers in this short guide before reading more.

To use the dot operator with pointers, you first need to create a pointer to an object and then, in order to access the element that it points to, we can use the dot operator such as (*ptr).name.

Notice how we are using two operators: we are using the asterisk operator to dereference the pointer (access the original Person instance it is pointing to) and next, we use the dot operator on the dereferenced/original instance as usual.

We can take a deeper look at this below:

#include <iostream>

class Person {
public:
    void setName(std::string newName) {
        name = newName;
    }
    std::string name;
};

int main() {
    Person *ptrPerson = new Person();
    (*ptrPerson).setName("John Smith");
    std::cout << "ptrPerson name is: "<< (*ptrPerson).name << std::endl;
    delete ptrPerson;
    return 0;
}

In this example, we create a pointer called ptrPerson that points to an instance of the Person class created using the new operator.

To call the setName member function and set the name to “John Smith”, we use asterisk operator with parentheses to dereference the pointer ptrPerson and then use the dot operator to access the member function. Similarly, to access the name variable, we use parentheses again to dereference the pointer and then use the dot operator.

It’s important to note that the pointer, ptrPerson, should be deleted after it’s use, to avoid memory leaks, thus the use of the delete keyword.

Keep in mind that we are using parentheses to overcome the strength of the dot operator. In other words, if we were to type *ptrPerson.name, we would get the following error:

error: request for member ‘name’ in ‘ptrPerson’, which is of pointer type ‘Person*’ (maybe you meant to use ‘->’ ?)

This is happening because the dot operator is first being called (before * dereferences the pointer), therefore this brings us to a very important property of the dot operator:

  • The dot operator cannot be used on pointers directly, it needs to be used on dereferenced pointers such as (*ptrPerson).name.

Additionally, it is important to note that the dot operator (.) cannot be overloaded unlike the arrow operator (->).

Arrow operator ->

Concept

The arrow operator (->) is used in C++ to access the members of a class or struct when working with pointers. The arrow operator is a shorthand for dereferencing a pointer and accessing a member of the object that the pointer points to.

In general, you use the arrow operator (->) in the following way:

pointer_name->member_name

For example, using the Person class we have been using in previous examples, if you have a pointer to an instance of the class, you can use the arrow operator to access the setName member function like this:

Person *ptrPerson = new Person();
ptrPerson->setName("John Smith");

This is equivalent to:

(*ptrPerson).setName("John Smith");

Here, the arrow operator -> is a shorthand for the dereference operator (*) and the dot operator (.) The above line is equivalent to dereferencing the pointer ptrPerson and then accessing the setName member function using the dot operator.

It’s important to note that the arrow operator can only be used on pointers (unlike the dot opertor), using it on non-pointer variables will result in a compile error.

Full example

#include <iostream>

class Person {
public:
    void setName(std::string newName) {
        name = newName;
    }
    std::string name;
};

int main() {
    Person *ptrPerson = new Person();
    ptrPerson->setName("John Smith");
    std::cout << "ptrPerson name is: "<< ptrPerson->name << std::endl;
    delete ptrPerson;
    return 0;
}

This code defines a simple Person class with a public member function setName and a public data member name. The setName function sets the value of the name variable to a new value passed as a parameter.

In the main function, the code creates a pointer to an instance of the Person class using the new operator. The pointer is called ptrPerson.

Then, using the arrow operator (->), the code calls the setName member function on the object pointed to by ptrPerson and sets its name variable to “John Smith”.

After that, the code uses the cout function to print the value of the name variable of the object pointed by ptrPerson, which is “John Smith”.

Finally, the code uses the delete keyword to free the memory allocated for the object pointed by the pointer ptrPerson, to avoid memory leaks.

It’s important to note that using the new operator will allocate memory dynamically, whereas an object created on the stack, does not need to be deleted, it will be deleted automatically when it goes out of scope. Also, it’s important to use smart pointers instead of raw pointers, to avoid memory leaks and simplify the code.

Conclusion

Briefly, it is important to note the main 4 differences between the dot operator (.) and the arrow operator (->)

  • The dot operator (.) is used to access the members of an object or struct when working with objects.
  • The arrow operator (->) is used to access the members of an object or struct when working with pointers.
  • The dot operator is used on objects and references, whereas the arrow operator is used on pointers only.
  • The dot operator is used to access the members of an object directly, whereas the arrow operator is used to access the members of an object by first dereferencing the pointer.