小编Xia*_*ENG的帖子

&C :: c和&(C :: c)之间有什么区别?

测试下面的代码,我把输出信息放在评论中.我使用的是gcc 4.8.5和Centos 7.2.

#include <iostream>
#include <cstdio>

class C 
{
    public:
        void foo() {
            printf("%p, %p\n", &C::c, &(C::c)); // output value is 0x4, 0x7ffc2e7f52e8
            std::cout << &C::c << std::endl;    // output value is 1
        }
        int a;
        int c;
};

int main(void)
{
    C co;

    printf("%p\n", &C::c);              // output value is 0x4
    std::cout << &C::c << std::endl;    // output value is 1

//    printf("%p\n", &(C::c));   // compile error, invalid use of non-static data member 'C::c'

    co.foo();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)
  1. 根据 …

c++

48
推荐指数
1
解决办法
2629
查看次数

标签 统计

c++ ×1