0

I have problem in this line std::cout << &X::a << std::endl; this line print 1 supposed to printed address of &X::a

this my code

#include <iostream>
#include <string>
#include <iomanip>

class X
{
public:
    int a;
    void f(int b);
};

void X::f(int b)
{
    std::cout << "the value of b is " << b << std::endl;
}

void add()
{
    std::cout << "Hello this function hello" << std::endl;
}
int main()
{
    void (*funcPtr)(void) = add;
    funcPtr();
    int X::*ptrnumber = &X::a;
    void (X::*ptrfunction)(int) = &X::f;
    X xobj;
    xobj.*ptrnumber = 10;
    std::cout << "the vslue of a is " << xobj.*ptrnumber << std::endl;
    (xobj.*ptrfunction)(20);
    std::cout << std::hex << &X::a << std::endl;
    //std::cout << funcPtr << std::endl;
}

i compiled with different complier c++ but i have same output
i displayed type of this "M1Xi" i don't know this data type

16
  • &X::a has no address. Commented Aug 5, 2023 at 17:56
  • 2
    X::a doesn't have an address; X is a type, not an object. If you write X x; then x.a will have an address. Commented Aug 5, 2023 at 17:56
  • 1
    bool overload is taken... Commented Aug 5, 2023 at 18:01
  • 1
    I asked chatGpt on the reason tel me it that is &X::a is address Commented Aug 5, 2023 at 18:11
  • 7
    ChatGPT is just a dumb chatbot. It is not an experienced C++ developer, who actually knows and understands C++. Commented Aug 5, 2023 at 18:20

2 Answers 2

3

&X::f and &X::a are not pointers. Because f and a are both non-static members, they are instead member pointers, which behave very differently from pointers regardless of the naming and syntax similarity.

A member pointer (whether function or data member) does not represent an address. So it doesn't make sense to try to print its address.

So there is also no overload for << with std::ostream in the standard library that would take a member pointer either.

However, there is an overload taking a bool as parameter and it is possible to implicitly convert a member pointer to bool. The result is false if the member pointer has the null member pointer value and true otherwise. With the default setting true is then printed as 1.

Sign up to request clarification or add additional context in comments.

3 Comments

i did't really get the sentence "A Pointer-to-member (whether function or data member) does not represent an address. So it doesn't make sense to try to print its address.", normally it means that this pointer holds the address of this data member?
@HassanLakhal No, a member pointer is not a pointer at all. It doesn't point to a concrete member subobject of an object. It does so only in combination with a pointer to the class object when applying the .* or ->* operators. The member pointer itself only abstractly identifies one member of a given class type among other members (of the same type) in the same (or inheritance-related) class, not as a concrete object of the class type.
@HassanLakhal I would recommend you to read up about member pointers in an introduction to the language, such as one of the recommended books. They have nothing to do with normal pointers and explaining what they are and how they are used wouldn't fit here.
1

The expression &X::a is not an address that you could use in an ordinary address. It is a relative address to a member. It can only be used with a pointer to member.

Pointer to members can be converted to other pointer to members and only with constraints. They cannot be converted to other kind of pointers or to int. It is difficult in this condition to print their value.

The only conversion possible is to a boolean, and it is meant to see if it is null or not. Because a pointer to member can be nullptr:

int X::*px = &X::a;
bool bx = px; std::cout << bx <<std::endl; 
px = nullptr; 
bool bn = px; std::cout << bn <<std::endl; 

If you want to print the address of the pointer to member, you must dereference it with an object:

std::cout << std::hex << &(xobj.*(&X::a)) << std::endl; 

As you may imagine, it is not possible to use tricks using dereferencing a nullptr to member or using a nullptr as object address, since dereferencing a null pointer is UB. By the way, this is not a real problem, because the standard makes very little guarantees about the layout of an object, so that this relative address is of little use on its own.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.