Dot and Scope Operators

The Dot Operator in C++

The dot operator (.) reads and sets the members of a C++ struct or class object and calls its member functions. Covers pointers, access rules and errors.

On this page
  1. The short answer: the dot connects an object to its members
  2. Reading and setting struct members
  3. Calling member functions with the dot
  4. Pointers: (*p).x and p->x
  5. Access control: public and private members
  6. Nested members and arrays of structs
  7. The dot in C and C++
  8. What the dot operator cannot do
  9. Common mistakes
  10. Key points to remember

The short answer: the dot connects an object to its members

The dot operator (.) in C++ connects an object to one of its members. A member is a variable or a function that belongs to a struct, class or union. You write the object’s name, a dot, and the member’s name:

object.member

The same dot works for member variables and member functions. What it produces depends on the member: a variable can be read or assigned to, and a function can be called.

Reading and setting struct members

A struct is a simple way to group related values. This example uses a struct with two integer members and sets them with the dot:

#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point p{};     // x and y start at 0
    p.x = 3;       // set the member x
    p.y = 4;       // set the member y

    std::cout << p.x << ", " << p.y << "\n";  // prints 3, 4
}

The {} in Point p{}; sets each member to zero. If you leave it out, a local int member holds whatever value was in that memory until you assign to it, so it is a good habit to initialize members.

Calling member functions with the dot

Members can also be functions. You call them with the same dot:

#include <iostream>
#include <string>

struct Person {
    std::string name;
    int age;

    void greet() const {
        std::cout << "Hi, I am " << name << "\n";
    }
};

int main() {
    Person alice{"Alice", 30};
    alice.greet();                           // prints: Hi, I am Alice
    std::cout << alice.age << "\n";          // prints: 30
    std::cout << alice.name.size() << "\n";  // prints: 5
}

The last line chains two dots. alice.name is a std::string, and size() is one of its member functions. A chain works as long as each step gives you an object that has the member you want next.

Pointers: (*p).x and p->x

The dot works on an object, not on a pointer to one. If you have a pointer, you can dereference it with * and then use the dot, or you can use the arrow operator (->):

Point p{3, 4};
Point* ptr = &p;

std::cout << (*ptr).x << "\n";   // dereference, then use the dot: prints 3
std::cout << ptr->x << "\n";     // arrow: same member, same result
// std::cout << ptr.x << "\n";   // compile error: ptr is a pointer, not an object

ptr->x is shorthand for (*ptr).x. The parentheses matter. The dot binds more tightly than unary *, so *ptr.x is read as *(ptr.x), which is not what you want here.

Access control: public and private members

Whether the dot can reach a member depends on its access level. Members of a struct are public by default, so the dot works on them from outside the struct. Members of a class are private by default, so the dot cannot reach them from outside unless you mark them public. For the full set of access keywords, see access modifiers in C++.

#include <iostream>

class Account {
public:
    void deposit(int amount) { balance += amount; }
    int current() const { return balance; }

private:
    int balance = 0;
};

int main() {
    Account a;
    a.deposit(50);                      // OK: public member function
    std::cout << a.current() << "\n";   // prints 50
    // a.balance = 1000;                // compile error: balance is private
}

The dot does not change the access rules. The compiler checks them when it builds the code.

Nested members and arrays of structs

A member can itself be a struct, so you can chain dots to reach a member deeper inside. Arrays of structs work the same way. Pick the element with [] first, then use the dot:

#include <iostream>
#include <string>

struct Point {
    int x;
    int y;
};

struct Address {
    std::string city;
};

struct Customer {
    std::string name;
    Address home;
};

int main() {
    Customer c{"Ben", Address{"Lisbon"}};
    std::cout << c.home.city << "\n";   // prints: Lisbon

    Point points[2] = {{1, 2}, {3, 4}};
    std::cout << points[1].x << "\n";   // prints: 3
}

points[1].x is read as (points[1]).x, because [] and the dot both work from left to right. For more on arrays of structs, see arrays of structs in C++.

The dot in C and C++

The dot works the same way in C. The main difference is how you name the type. In C you normally declare a struct variable with the struct keyword, as in struct Point p;, while C++ lets you write Point p;. Unions use the dot in both languages.

What the dot operator cannot do

  • You cannot overload the dot. Operator overloading lets a class define how many operators behave. The dot is not on the list of operators that can be overloaded. Neither are the scope resolution operator (::), pointer-to-member access (.*) and the conditional operator (?:). For how overloading works for the others, see operator overloading in C++.
  • It does not pick a namespace or a class. That is the job of ::. For example, std::cout uses ::, and std.cout does not compile. For more on that operator, see What Does :: Mean in C++?
  • It does not work on a pointer. Use -> or (*p). instead, as shown above.

Common mistakes

  • Using the dot on a pointer, such as ptr.x, instead of ptr->x.
  • Writing *p.x when you meant (*p).x or p->x.
  • Trying to read or write a private member from outside the class.
  • Using :: where the dot belongs, as in p::x. The double colon is for scope, not for an object’s members.
  • Calling a data member as if it were a function, as in p.x() when x is an int.

Key points to remember

  • The dot connects an object, not a pointer, to its member variables and member functions.
  • ptr->member means the same as (*ptr).member.
  • Struct members are public by default. Class members are private by default.
  • For an array of structs, use [] first, then the dot: points[i].x.
  • The dot cannot be overloaded.

Keep exploring

Guides from other topics

All topics →

stringstream in C++

stringstream in C++ explained: how the <sstream> classes read and write text in memory, convert strings and numbers, split text, and avoid common mistakes.

stringstream 4 min read