[ create a new paste ] login | about

Link: http://codepad.org/QxT3AlYe    [ raw code | output | fork ]

C++, pasted on Jul 9:
class C
{
public:
    int a;
    int b;
};

int main()
{
    // make pointer to member, initially pointing to a
    int C::*ptrToMember = &C::a;

    C x = {10, 11};
    C *ptrToObj = &x; // make pointer to object x

    cout << "a initial value: " << ptrToObj->*ptrToMember << endl;

    ptrToObj->*ptrToMember = 2; // store in a;

    cout << "a after change: " << ptrToObj->*ptrToMember << endl;

    ptrToMember = &C::b; // change pointer to b

    cout << "b initial value: " << ptrToObj->*ptrToMember << endl;

    ptrToObj->*ptrToMember = 3; // store in b;

    cout << "b after change: " << ptrToObj->*ptrToMember << endl;


    int* ptr = &(ptrToObj->a);
    cout << "ptr to a: " << *ptr << endl;

    ptr = &(ptrToObj->b);
    cout << "ptr to b: " << *ptr << endl;

}


Output:
1
2
3
4
5
6
a initial value: 10
a after change: 2
b initial value: 11
b after change: 3
ptr to a: 2
ptr to b: 3


Create a new paste based on this one


Comments: