j***i 发帖数: 1278 | 1 I read the insider the c++ Object model 3.6
It says
class Point3d {
public:
virtual ~Point3d();
// ...
protected:
static Point3d origin;
float x, y, z;
};
The physical offset of the three coordinate members within the class
layout are, respectively, either 0, 4, and 8 if the vptr is placed at the
end or 4, 8, and 12 if the vptr is placed at the start of the class. The
value returned from taking the member's address, however, is always bumped
up by 1. Thus the actual values are 1, 5, | X****r 发帖数: 3557 | 2 You did it wrong when trying to print out these values of
pointer-to-member type. Try this way:
(assuming sizeof(float Point3d::*) == sizeof(unsigned long) here;
if not, change 'unsigned long' to appropriate integral type)
float Point3d::*p = &Point3d::x;
std::cout << "&Point3d::x = " << *(unsigned long*)&p << std::endl;
The reason why you got '1's if you try to print out
pointer-to-member values directly is because they can be
automatically convert to boolean (true for not null, false for
null)
【在 j***i 的大作中提到】 : I read the insider the c++ Object model 3.6 : It says : class Point3d { : public: : virtual ~Point3d(); : // ... : protected: : static Point3d origin; : float x, y, z; : };
| X****r 发帖数: 3557 | 3 Also, peeking at pointer-to-member is not a good way to
reveal memory layout of an object. For example, whether or not
the internal value of pointer-to-member addresses are bumped by
one are compiler-dependent -- this is used to allow a null pointer
to be converted to a 'null' pointer-to-member. But there other
ways to designate a 'null' pointer-to-member, e.g. ~0, in
which case bumping by 1 becomes unnecessary.
A simpler yet more reliable way to look at memory layout of
an object is to directly
【在 j***i 的大作中提到】 : I read the insider the c++ Object model 3.6 : It says : class Point3d { : public: : virtual ~Point3d(); : // ... : protected: : static Point3d origin; : float x, y, z; : };
| j***i 发帖数: 1278 | 4 Thanks it works
can you explain it a little | X****r 发帖数: 3557 | 5 Yes I know it is confusing, so I have modified my post to explain
a little more. Just take another look.
【在 j***i 的大作中提到】 : Thanks it works : can you explain it a little
| h****8 发帖数: 599 | 6 大哥你为什么这么牛啊
有什么推荐书目么
我现在只是看完了effective c++, primer, 还有modern c++ design
因为不是科班出身 还请大哥指点指点
【在 X****r 的大作中提到】 : Yes I know it is confusing, so I have modified my post to explain : a little more. Just take another look.
| t****t 发帖数: 6806 | 7 要一边看书一边写. 只看不写的话, 看了后面, 前面很快就忘光光了
【在 h****8 的大作中提到】 : 大哥你为什么这么牛啊 : 有什么推荐书目么 : 我现在只是看完了effective c++, primer, 还有modern c++ design : 因为不是科班出身 还请大哥指点指点
| h****8 发帖数: 599 | 8 是的是的 不过就拿楼主的这个问题来说,要不是如此熟悉stl的结构,怎么会知道cout
会把指针转换成bool呢
【在 t****t 的大作中提到】 : 要一边看书一边写. 只看不写的话, 看了后面, 前面很快就忘光光了
| t****t 发帖数: 6806 | 9 cout(其实是<<)对数据有定义, 对别的有没有定义一般我是查手册的
cout
【在 h****8 的大作中提到】 : 是的是的 不过就拿楼主的这个问题来说,要不是如此熟悉stl的结构,怎么会知道cout : 会把指针转换成bool呢
|
|