Is printing a member pointer a user-defined

Suppose I have this code:

#include <iostream> struct Mine { int a; int b; }; int main() { int Mine::* memberPointerA = &Mine::a; int Mine::* memberPointerB = &Mine::b; std::cout << memberPointerA; std::cout << "\n"; std::cout << memberPointerB; } 

When I run this using Microsoft Visual C ++ (2015)

I get the following output

 1 1 

The result I expect looks something like this:

 1 2 

So this asks the question: is this printing the element pointer in a certain way?

+5
source share
2 answers

There is defined conversion from pointer to bool . Since element variable pointers are not NULL , they are evaluated as true and printed as 1 .

+12
source

The key problem is that a member pointer cannot be converted to void* , which is what overloading does, which usually processes print pointers. A.

Thus, the following best transform is used, which is a pointer to a transform β†’ bool . Both pointers are not null pointers, so you get the output you see.

If you try to print β€œregular” pointers (as opposed to pointers to an element), you will get some output along the lines of what you originally expected.

+7
source

Source: https://habr.com/ru/post/1270302/


All Articles