为什么通过指针静态访问类数据成员返回1?

Rou*_*uki 20 c++

给出以下代码:

#include <iostream>

using std::cout;

class A {
    public:
        virtual ~A() {}
        int x,y,z;
};

int main (void)
{
    std::cout<<&A::x;
    std::cout<<&A::y;
    std::cout<<&A::z;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

111

输出是什么意思?为什么是1?是否有任何理由通过指针访问类成员(没有创建对象)?

编辑 - 使用:

printf("%p",&A::x);
printf("%p",&A::y);
printf("%p",&A::z);
Run Code Online (Sandbox Code Playgroud)

打印:4,8和C.

现在我觉得更有意义..(字节)但是,还有什么用呢?

GMa*_*ckG 30

没有operator<<(std::ostream&, T)定义T = &C::m.通常你会收到一个错误.

但相反,有一个for T = bool和一个从成员指针隐式转换bool.所以你看到的输出只是那些指针不为空(被转换为true)的结果.

试试这个,例如:

#include <iomanip>
#include <iostream>

struct A
{
    int x, y, z;
};

int main()
{
    std::cout << std::boolalpha; // print boolean values as text

    std::cout << &A::x << std::endl;
    std::cout << &A::y << std::endl;
    std::cout << &A::z << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:




请注意,在代码中printf("%p", &A::X),您有未定义的行为.

%p说明符的值必须是a void*,并且不存在从成员指针到a的转换void*.相反,你有什么别名(类型双关语)void*,这是未定义的行为.(想象一下,这sizeof(&A::x)是4而sizeof(void*)64岁;没有人说这不可能是这种情况.)

你只需要接受这样的想法:并非所有指针都可以被视为整数偏移.我们甚至可以打印一个指针是实现定义的:它可以打印"apple"为null,"pear"为一个值,"milk"为另一个(如果(哑)实现想要).我之前已经触及过价值观和表现形式之间的差异.

在这种情况下,没有输出值在所有.这没关系,并非所有值都有有意义的打印输出.你能做的最多就是打印出各个位:

#include <climits>
#include <iostream>
#include <type_traits>

template <typename T>
auto print_bits(const T& value)
    -> typename std::enable_if<std::is_standard_layout<T>::value>::type
{
    // it's okay to alias a standard-layout type as a sequence of bytes:
    const auto valueAsBytes = reinterpret_cast<const unsigned char*>(&value);

    for (std::size_t byte = 0; byte < sizeof(T); ++byte)
    {
        // print in reverse order
        const std::size_t byteIndex = sizeof(T) - byte - 1;

        const unsigned char byteValue = valueAsBytes[byteIndex];

        for (std::size_t bit = 0; bit < CHAR_BIT; ++bit)
        {
            // print in reverse order
            const std::size_t bitIndex = CHAR_BIT - bit - 1;

            const bool bitValue = (byteValue & (1U << bitIndex)) != 0;

            std::cout << (bitValue ? 1 : 0);
        }

        std::cout << ' ';
    }

    std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

(我以相反的顺序打印字节和位,因为在我的体系结构中,这会将最低有效位放在右边.我更喜欢以这种方式查看二进制值.)

这给出了:

struct A
{
    int x, y, z;
};

int main()
{
    // example:
    for (unsigned i = 0; i < 64; ++i)
        print_bits(i);

    std::cout << std::endl;

    // member-pointers:
    print_bits(&A::x);
    print_bits(&A::y);
    print_bits(&A::z);
}
Run Code Online (Sandbox Code Playgroud)

输出:

00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
[...]
00000000 00000000 00000000 00111101
00000000 00000000 00000000 00111110
00000000 00000000 00000000 00111111

00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000100
00000000 00000000 00000000 00001000

您对成员指针的看法无法保证.