Last updated:2026-10-12
C++ Dereference Operator *¶
0. Introduction¶
When we work with pointers, we usually use *, for example:
Beyond that, does it have any other uses?
The answer is yes; for example, in C++ there are also the following three:
::*.*->*
So do you know what each of these three means?
1. ::*¶
::* is called the Pointer to member declarator.
Let me give an example:
In the example below, how would you take the address of w_?
If you write double * d = &w_; that is invalid, because it is not known where w_ lives, so you need a pointer to member declarator; that makes the binding possible, and once you have this pointer you can access the member. Let's continue with the other two.
class Rectangle {
public:
double w_, h_;
Rectangle(double w, double h) : w_(w), h_(h) {}
double area() { return w_ * h_; }
};
int main() {
Rectangle r{2.1, 3.2};
double Rectangle::* w = &Rectangle::w_;
r.*w = 2.2;
std::cout << r.w_ << std::endl;
};
2. .*¶
.* is called pointer to member of object. This one is fairly easy to understand: it is the pointer for accessing a member of an object; a pointer to member lets you refer to non-static members of a class object. You cannot use a pointer to member to point to a static class member, because the address of a static member is not associated with any particular object.
So, in the example above, this line of code changes the value of member w to 2.2.
3. ->*¶
->* is called pointer to member of pointer. This one is even easier to understand: it is similar to ., except that it operates on the address of a member of a **pointer object*,
so the corresponding example becomes:
For more details, please read cppreference.com.
https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_members
Did you get it? End of this section~
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Constants and Qualifiers | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事
- C++ restrict and Compiler Optimization | C++那些事