In Mr Kenny Kerr’s this column, he defined a struct and a typedef like this:
struct boolean_struct { int member; };
typedef int boolean_struct::* boolean_type;
Then what is the meaning of this typedef?
Another question is concerning the following code:
operator boolean_type() const throw()
{
return Traits::invalid() != m_value ? &boolean_struct::member : nullptr;
}
What is the meaning of “&boolean_struct::member” ?
The
typedefcreates a type calledboolean_typewhich is equivalent to a pointer to anintmember inside aboolean_structobject.It’s not the same thing to a pointer to an
int. The difference is that an object ofboolean_typerequires aboolean_structobject in order to dereference it. A normal pointer to anintdoes not. The best way to see how this is different is via some code examples.Consider only normal pointers to
ints:Now consider if we used pointers to
intmembers inside aboolean_struct:As you can see, the syntax and their behavior are different.
This returns the address of the
membervariable inside aboolean_struct. See above code example.