7.3. Pointers to classes¶
When we create a pointer to a class, member access changes. Given a simple POD:
struct Fibonacci {
short f5; short f6; short f7;
};
Access to members of any objects created uses the member access operator
operator .
:
// create foo with initial values
Fibonacci foo = {5, 8, 13};
// use member access operator to get values
std::cout << "The fifth, sixth and seventh Fibonacci numbers are: "
<< foo.f5 << ", "
<< foo.f6 << ", and "
<< foo.f7 << ".\n";
When you need to access members through a pointer,
the operator precedence rules for pointer dereference
and member access are a common source of error.
Let’s add a pointer to foo
:
// create foo with initial values
Fibonacci foo = {5, 8, 13};
Fibonacci* bar = &foo;
It seems that if foo.f5
works, then
given a pointer to a foo
, that *bar.f5
should work, but it does not.
The member access operator has higher precedence than
the dereference operator.
The code *bar.f5
is equivalent to *(bar.f5)
.
This is almost always a bug.
In this case, f5
is not a pointer type and cannot be dereferenced.
Explicit use of parentheses is one way to fix this problem:
(*bar).f5
This works, but the syntax is ugly.
For this reason, the operator ->
is used to
access members of a pointer to an object.
The code bar->f5
is easier to read than (*bar).f5
.
Putting it all together:
// create foo with initial values
Fibonacci foo = {5, 8, 13};
Fibonacci* bar = &foo;
// This does not compile!
// member access operator has higher precedence than
// dereference operator
std::cout << "F5: " << *bar.f5; // can't dereference f5
// Use this:
std::cout << "F6: " << (*bar).f6;
// or even better, this:
std::cout << "F7: " << bar->f7;
More to Explore
From cppreference.com