9

我正在玩指向成员的指针,并决定实际打印指针的值。结果出乎我的意料。

#include <iostream>

struct ManyIntegers {

    int a,b,c,d;
};

int main () {

    int ManyIntegers::* p;

    p = &ManyIntegers::a;
    std::cout << "p = &ManyIntegers::a = " << p << std::endl; // prints 1

    p = &ManyIntegers::b;
    std::cout << "p = &ManyIntegers::b = " << p << std::endl; // prints 1

    p = &ManyIntegers::c;
    std::cout << "p = &ManyIntegers::c = " << p << std::endl; // prints 1

    p = &ManyIntegers::d;
    std::cout << "p = &ManyIntegers::d = " << p << std::endl; // prints 1

    return 0;
}

为什么值p总是1?的值不应该p以某种方式反映它指向的类成员吗?

4

6 回答 6

9

正如大家所说,ostream没有适当的operator<<定义。

试试这个:

#include <cstddef>
#include <iostream>

struct Dumper {
  unsigned char *p;
  std::size_t size;
  template<class T>
  Dumper(const T& t) : p((unsigned char*)&t), size(sizeof t) { }
  friend std::ostream& operator<<(std::ostream& os, const Dumper& d) {
    for(std::size_t i = 0; i < d.size; i++) {
      os << "0x" << std::hex << (unsigned int)d.p[i] << " ";
    }
    return os;
  }
};

#include <iostream>

struct ManyIntegers {

    int a,b,c,d;
};

int main () {

    int ManyIntegers::* p;

    p = &ManyIntegers::a;
    std::cout << "p = &ManyIntegers::a = " << Dumper(p) << "\n"; 

    p = &ManyIntegers::b;
    std::cout << "p = &ManyIntegers::b = " << Dumper(p) << "\n"; 

    p = &ManyIntegers::c;
    std::cout << "p = &ManyIntegers::c = " << Dumper(p) << "\n"; 

    p = &ManyIntegers::d;
    std::cout << "p = &ManyIntegers::d = " << Dumper(p) << "\n"; 

    return 0;
}
于 2012-08-24T13:48:19.093 回答
7

标准 ostreamoperator<<对指向成员的指针没有重载,因此您的指针已隐式转换为bool.

于 2012-08-24T13:35:48.117 回答
3

p实际上包含对象中的偏移量。bool如果它们确实分别包含某些偏移量,则打印它们会打印隐式转换值 true 或 false。转换的发生是因为ostream's 的插入成员对指向成员的指针没有任何重载。

于 2012-08-24T13:36:51.650 回答
2

没有operator<<将指向成员的指针作为参数的重载。因此,如果您尝试打印指向成员的指针,它会隐式转换成true哪个被传递给bool作为参数的重载,并且它打印1对应于true.

如果您使用std::boolalpha流操纵器,它将打印true而不是1

std::cout << std::boolalpha << "p = &ManyIntegers::a = " << p ;
           //^^^^^^^^^^^^^^

输出(见ideone):

p = &ManyIntegers::a = true

于 2012-08-24T13:37:28.000 回答
0

成员指针不一定是数值,通常它是结构或类似的东西。我认为没有办法从成员指针中获取值,但即使有,我也看不出这会有什么用。

于 2012-08-24T13:46:44.867 回答
0

这是一个完全符合标准的实现,用于显示指向成员的指针的内存表示:

#include <iostream>
#include <iomanip>

template<int... I> struct index_tuple { using succ = index_tuple<I..., sizeof...(I)>; };
template<int I> struct indexer { using type = typename indexer<I - 1>::type::succ; };
template<> struct indexer<0> { using type = index_tuple<>; };
template<typename T> typename indexer<sizeof(T)>::type index(const T &) { return {}; }

template<typename T> class dumper {
    unsigned char buf[sizeof(T)];
    friend std::ostream &operator<<(std::ostream &os, const dumper &o) {
        std::ios_base::fmtflags flags{os.flags()};
        std::copy_n(o.buf, sizeof(T),
            std::ostream_iterator<int>(os << std::hex << std::showbase, " "));
        return os << std::setiosflags(flags);
    }
    template<int... I> dumper (const T &t, index_tuple<I...>):
        buf{reinterpret_cast<const unsigned char *>(&t)[I]...} {}
public:
    dumper(const T &t): dumper(t, index(t)) {}
};
template<typename T> dumper<T> dump(const T &t) { return {t}; }

struct ManyIntegers {
    int a,b,c,d;
};

int main () {
    std::cout << "p = &ManyIntegers::a = " << dump(&ManyIntegers::a) << std::endl;
    std::cout << "p = &ManyIntegers::b = " << dump(&ManyIntegers::b) << std::endl;
    std::cout << "p = &ManyIntegers::c = " << dump(&ManyIntegers::c) << std::endl;
    std::cout << "p = &ManyIntegers::d = " << dump(&ManyIntegers::d) << std::endl;
}

输出如预期:

p = &ManyIntegers::a = 0 0 0 0 
p = &ManyIntegers::b = 0x4 0 0 0 
p = &ManyIntegers::c = 0x8 0 0 0 
p = &ManyIntegers::d = 0xc 0 0 0 
于 2012-08-24T14:44:23.347 回答