SoFunction
Updated on 2025-03-04

The difference between pointer and object member access: `.` and `->`

When learning C++, you often encounter two symbols that access object members:.and->. These two symbols may seem simple, but their correct use requires understanding of the essential differences between pointers and objects. For C/C++ novices, this article will explain their differences in detail to help you avoid pitfalls when programming.

one,.and->Basic concepts

In C++,.and->Is an operator used to access the member of a class (or structure). They are used in different scenarios:

  • .(dot operator): used to access members of non-pointer objects.
  • ->(arrow operator): Used to access the member pointed to by the pointer.

The two operators have similar functions, but they apply different object types.

two,. Usage

Point operator.is the simplest member access operator, suitable for ordinary objects. Through the object name and dot number, we can directly access the object's member functions or member variables.

Sample code

#include <iostream>
using namespace std;
class Stack {
public:
    void enstack(int value) {
        cout << "Value enstacked: " << value << endl;
    }
};
int main() {
    Stack s;      // Create a normal object    (1); // Access member functions via .    return 0;
}

Output result

Value enstacked: 1

Here,sIt's a normal object,(1)Direct call object by dotsofenstackfunction.

Summary If the variable you create is a normal object (stack allocation or static allocation), use.Access its members. The dot operator can only be used for non-pointing objects.

three,->Usage

Arrow operator->Used to point to the image. Its function is to implicitly dereference the pointer and access members of the object pointed to by the pointer.

Sample code

#include <iostream>
using namespace std;
class Stack {
public:
    void enstack(int value) {
        cout << "Value enstacked: " << value << endl;
    }
};
int main() {
    Stack *s = new Stack(); // Create a pointer icon    s->enstack(1);          // Access member functions via ->    delete s; // Free dynamically allocated memory    return 0;
}

Output result

Value enstacked: 1

Here,sIt's a pointerStackpointer to the object,s->enstack(1)Implicitly dereference the pointer and access its member functionsenstack

Summarize

  • If you are using targeted,->Access its members.
  • The arrow operator is equivalent to(*pointer).memberabbreviation of .

Four,. and->The equivalent relationship

->Actually it is(*pointer).memberabbreviation of . This can be understood through the following code:

Sample code

#include <iostream>
using namespace std;
class Stack {
public:
    void enstack(int value) {
        cout << "Value enstacked: " << value << endl;
    }
};
int main() {
    Stack *s = new Stack();
    // Use the -> operator    s->enstack(1);
    // Equivalent to using dereferences and dot numbers    (*s).enstack(1);
    delete s; // Free memory    return 0;
}

Output result

Value enstacked: 1
Value enstacked: 1

From this code, we can see thats->enstack(1)and(*s).enstack(1)It is completely equivalent.

Why there is->

If not->, We need to dereference the pointer first and then use dot numbers to access members, which will appear lengthy to write. For example:

(*pointer).memberFunction();

and->This process is simplified directly, and the code is simpler:

pointer->memberFunction();

5. How to determine which operator to use?

Check variable types

  • If it is a normal object, use.
  • If it is targeted, use->

Error message
If you try to use the pointer object., or use it on normal objects->, the compiler will report an error:

  • typedoes not have member…” (type error).
  • Or "invalid use of member..." sample code

Here are common error examples:

Stack *s = new Stack();
// Error: Pointer cannot be accessed directly with dot numbers(1);
// Correct: Use arrow operators->enstack(1);

6. Practical comparison examples

Here is a comprehensive example showing how to use it in different situations.and->

Sample code

#include <iostream>
using namespace std;
class Stack {
public:
    void enstack(int value) {
        cout << "Value enstacked: " << value << endl;
    }
};
int main() {
    // Normal object    Stack obj;
    (10);
    // Point to the target    Stack *ptr = new Stack();
    ptr->enstack(20);
    delete ptr; // Free memory    return 0;
}

Output result

Value enstacked: 10
Value enstacked: 20

7. Things to note

Pointer initialization

  • use->Before, make sure the pointer is initialized and points to a valid object, otherwise undefined behavior will result.

Memory management

  • For dynamically allocated objects, remember to usedeleteFree up memory, otherwise it will cause memory leaks.

Smart pointer

  • In modern C++, smart pointers are recommended (e.g.std::shared_ptrorstd::unique_ptr) to manage pointers and reduce the risk of manually managing memory.

8. Summary

  • Point operator.Used for normal objects.
  • Arrow operator->Used to point to an object and implicitly complete the reference operation.
  • There are clear usage scenarios between them and equivalent conversion can be achieved through dereference.

Understanding these basic concepts can prevent mistakes when accessing object members.

This article about the difference between accessing C++ pointer and object member: `.` and `->` is introduced here. For more related accessing content for accessing C++ pointer and object member, please search for my previous article or continue browsing the following related articles. I hope everyone will support me in the future!