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" ?
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?
The typedef
creates a type called boolean_type
which is equivalent to a pointer to an int
member inside a boolean_struct
object.
It's not the same thing to a pointer to an int
. The difference is that an object of boolean_type
requires a boolean_struct
object in order to dereference it. A normal pointer to an int
does not. The best way to see how this is different is via some code examples.
Consider only normal pointers to int
s:
struct boolean_struct { int member; };
int main()
{
// Two boolean_struct objects called bs1 and bs2 respectively:
boolean_struct bs1;
boolean_struct bs2;
// Initialize each to have a unique value for member:
bs1.member = 7;
bs2.member = 14;
// Obtaining a pointer to an int, which happens to be inside a boolean_struct:
int* pi1 = &(bs1.member);
// I can dereference it simply like this:
int value1 = *pi1;
// value1 now has value 7.
// Obtaining another pointer to an int, which happens to be inside
// another boolean_struct:
int* pi2 = &(bs2.member);
// Again, I can dereference it simply like this:
int value2 = *pi2;
// value2 now has value 14.
return 0;
}
Now consider if we used pointers to int
members inside a boolean_struct
:
struct boolean_struct { int member; };
typedef int boolean_struct::* boolean_type;
int main()
{
// Two boolean_struct objects called bs1 and bs2 respectively:
boolean_struct bs1;
boolean_struct bs2;
// Initialize each to have a unique value for member:
bs1.member = 7;
bs2.member = 14;
// Obtaining a pointer to an int member inside a boolean_struct
boolean_type pibs = &boolean_struct::member;
// Note that in order to dereference it I need a boolean_struct object (bs1):
int value3 = bs1.*pibs;
// value3 now has value 7.
// I can use the same pibs variable to get the value of member from a
// different boolean_struct (bs2):
int value4 = bs2.*pibs;
// value4 now has value 14.
return 0;
}
As you can see, the syntax and their behavior are different.
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" ?
This returns the address of the member
variable inside a boolean_struct
. See above code example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With