These statements turn the bit off, regardless of its prior state. First, the operator ~bit produces an integer with all its bits set to 1
Here’s a briefer way of doing the same thing:
lottabits &= ~bit;
Testing a Bit Value
Suppose you want to determine whether the bit corresponding to bit is set to 1 in lottabits. The following test does not necessarily work:
if (lottabits == bit) // no good
That’s because even if the corresponding bit in lottabits is set to 1, other bits might also be set to 1. The equality above is true
if (lottabits & bit == bit) // testing a bit
Real-world programmers often simplify this test to the following:
if (lottabits & bit) // testing a bit
Because bit consists of one bit set to 1 and the rest set to 0, the value of lottabits & bit is either 0 (which tests as false) or bit, which, being nonzero, tests as true.
Member Dereferencing Operators
C++ lets you define pointers to members of a class. These pointers involve special notations to declare them and to dereference them. To see what’s involved, let’s start with a sample class:
class Example
{
private:
int feet;
int inches;
public:
Example();
Example(int ft);
~Example();
void show_in() const;
void show_ft() const;
void use_ptr() const;
};
Consider the inches member. Without a specific object, inches is a label. That is, the class defines inches as a member identifier, but you need an object before you actually have memory allocated:
Example ob; // now ob.inches exists
Thus, you specify an actual memory location by using the identifier inches in conjunction with a specific object. (In a member function, you can omit the name of the object, but then the object is understood to be the one pointed to by the pointer.)
C++ lets you define a member pointer to the identifier inches like this:
int Example::*pt = &Example::inches;
This pointer is a bit different from a regular pointer. A regular pointer points to a specific memory location. But the pt pointer doesn’t point to a specific memory location because the declaration doesn’t identify a specific object. Instead, the pointer pt identifies the location of inches member within any Example object. Like the identifier inches, pt is designed to be used in conjunction with an object identifier. In essence, the expression *pt assumes the role of the identifier inches. Therefore, you can use an object identifier to specify which object to access and the pt pointer to specify the inches member of that object. For example, a class method could use this code:
int Example::*pt = &Example::inches;
Example ob1;
Example ob2;
Example *pq = new Example;
cout << ob1.*pt << endl; // display inches member of ob1
cout << ob2.*pt << endl; // display inches member of ob2
cout << po->*pt << endl; // display inches member of *po
Here .* and ->* are
Changing the object in the preceding example changes which inches member is used. But you can also change the pt pointer itself. Because feet is of the same type as inches, you can reset pt to point to the feet member instead of the inches member; then ob1.*pt will refer to the feet member of ob1:
pt = &Example::feet; // reset pt
cout << ob1.*pt << endl; // display feet member of ob1
In essence, the combination *pt takes the place of a member name and can be used to identify different member names (of the same type).
You can also use member pointers to identify member functions. The syntax for this is relatively involved. Recall that declaring a pointer to an ordinary type void function with no arguments looks like this:
void (*pf)(); // pf points to a function
Declaring a pointer to a member function has to indicate that the function belongs to a particular class. Here, for instance, is how to declare a pointer to an Example class method:
void (Example::*pf)() const; // pf points to an Example member function
This indicates that pf can be used the same places that Example method can be used. Note that the term Example: :*pf has to be in parentheses. You can assign the address of a particular member function to this pointer:
pf = &Example::show_inches;