Pointers to Class Members. McGraw-Hill The Complete Reference Fourth Edition
Quảng cáo
Quảng cáo
C h a p t e r 1 3 : A r r a y s , P o i n t e r s , R e f e r e n c e s , a n d t h e D y n a m i c A l l o c a t i o n O p e r a t o r s
337
Pointers to Class Members
C++ allows you to generate a special type of pointer that "points" generically to a member of a class, not to a specific instance of that member in an object. This sort of pointer is called a pointer to a class member or a pointer-to-member, for short. A pointer to a member is not the same as a normal C++ pointer. Instead, a pointer to a member provides only an offset into an object of the member's class at which that member can be found. Since member pointers are not true pointers, the . and –> cannot be applied to them. To access a member of a class given a pointer to it, you must use the special pointer-to-member operators .* and –>*. Their job is to allow you to access a member of a class given a pointer to that member.
Here is an example:
#include <iostream> using namespace std; class cl { public: cl(int i) { val=i; } int val;
}; int double_val() { return val+val; } int main()
{ int cl::*data; // data member pointer int (cl::*func)(); // function member pointer cl ob1(1), ob2(2); // create objects data = &cl::val; // get offset of val func = &cl::double_val; // get offset of double_val() cout << "Here are values: "; cout << ob1.*data << " " << ob2.*data << "\n"; cout << "Here they are doubled: "; cout << (ob1.*func)() << " "; cout << (ob2.*func)() << "\n";
} return 0;
338
C + + : T h e C o m p l e t e R e f e r e n c e
In main( ), this program creates two member pointers: data and func. Note carefully the syntax of each declaration. When declaring pointers to members, you must specify the class and use the scope resolution operator. The program also creates objects of cl called ob1 and ob2. As the program illustrates, member pointers may point to either functions or data. Next, the program obtains the addresses of val and
double_val( )
. As stated earlier, these “addresses” are really just offsets into an object of type cl, at which point val and double_val( ) will be found. Next, to display the values of each object's val, each is accessed through data. Finally, the program uses
func
to call the double_val( ) function. The extra parentheses are necessary in order to correctly associate the .* operator.
When you are accessing a member of an object by using an object or a reference
(discussed later in this chapter), you must use the .* operator. However, if you are using a pointer to the object, you need to use the –>* operator, as illustrated in this version of the preceding program:
#include <iostream> using namespace std; class cl { public: cl(int i) { val=i; } int val;
}; int double_val() { return val+val; } int main()
{ int cl::*data; // data member pointer int (cl::*func)(); // function member pointer cl ob1(1), ob2(2); // create objects cl *p1, *p2; p1 = &ob1; // access objects through a pointer p2 = &ob2; data = &cl::val; // get offset of val func = &cl::double_val; // get offset of double_val() cout << "Here are values: "; cout << p1->*data << " " << p2->*data << "\n"; cout << "Here they are doubled: ";
Tải xuống
Quảng cáo