Little question about ->* and .* (pointer-to-members)

Hello.

Here is a simple example for my question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Person
{
	int age;

	void print()
	{
		cout << "Age is: " << age << endl;
	}
};

int main(int argc, char** argv)
{
	int Person::* ptrAge = &Person::age;

	Person p;
	p.age = 10;
	p.print();

	p.*ptrAge = 20;
	p.print();

	_getch();
	return 0;
}


Very simple.
It works as I expected.

The questions are:

1. Why can't I print the value of the pointer? (compile-time error!)

1
2
3
cout << *ptrAge;

// error: operand of '*' must be a pointer 


2. Why, whenever I try to print the address of the pointer, I always get 1?

1
2
3
cout << ptrAge;

// output: 1 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

struct A
{
    int mf( int a ) { return m += a ; }
    int m = 0 ;
};

int fn( int a ) { return a*2 ; }

int main()
{
    A a ;

    // a. pointer to object (T* where T is an object type)
    auto pa = std::addressof(a) ; // pointer to A
    auto& ra = *pa ; // dereference (unary * operator): type of *pa is A, value category is lvalue
    bool b = pa ; // implicit conversion to bool

    void* pv = pa ; // implicit conversion to 'pointer to void'

    // *pv ; // *** error: pv is not a 'pointer to object'

    std::cout << pv << '\n' ; // ostream::operator<< ( const void* )
    std::cout << pa << '\n' ; // implicit conversion from 'pointer to A' to 'pointer to const void'

    //-------------------------------------------
    // b. pointer to function (F* where F is a non-member or static member function)
    using fn_type = int(int) ;
    fn_type* pfn = &fn ; // pointer to function
    pfn = fn ; // implicit conversion: 'function' to 'pointer to function'
    b = pfn ; // implicit conversion: 'pointer to function' to bool
    // pv = pfn ; // *** error: type of pfn is not a pointer to object type

    auto& rfn = *pfn ; // dereference (unary * operator): type of rfn is 'reference to function'

    std::cout << b << '\n' ; // ostream::operator<< ( bool )
    std::cout << pfn << '\n' ; // implicit conversion from 'pointer to function' to 'bool' (true)
    pfn = nullptr ;
    std::cout << pfn << '\n' ; // implicit conversion from 'pointer to function' to 'bool' (false)

    //-------------------------------------------
    // c. pointer to member T::*m (where T is a class type)

    auto pmo = &A::m ; // pointer to member object
    auto pmf = &A::mf ; // pointer to member function

    // *pmf ; // *** error: type of pmf is not a pointer to object type
    // pv = pmf ; // *** error: type of pmf is not a pointer to object type

    b = pmf ; // implicit conversion to bool
    std::cout << b << '\n' ; // ostream::operator<< ( bool )
    std::cout << pmf << '\n' ; // implicit conversion from 'pointer to member function' to 'bool'

    // pointer to member operators .* ->*
    a.*pmo = 7 ;
    (pa->*pmf)(23) ;
}

http://rextester.com/XUUM13355
Topic archived. No new replies allowed.