What value will we get if we apply std :: cout to a member pointer

We can get the address representation of an object in memory using

std::cout << &obj << std::endl

I am trying to do the same with a pointer-member type.

#include <iostream>

using namespace std;
struct X
{
    bool b;
    int a;
};
int X::* a =&X::a;
bool X::* b = &X::b;
X x;

int main()
{
    cout << a << endl << b; //1
                            //1
}

Demo

You see what I got 1. What is 1? Or should I mention pointer-to-memberhow simple a type is that is not directly tied to a "simple" pointer?

+4
source share
2 answers

Since there is no overload for operator<<, which takes a pointer to the element directly, aand is bimplicitly converted to boolfor which there is an overload. The semantics of this standard conversion are as follows:

, , prvalue bool. , false; true.

, a b true, 1.

+6

:

int main()
{
    cout << std::boolalpha << a << endl << b; //1
}

, "". bool.

+4

All Articles