English 中文(简体)
多重继承、基础班级点和规模
原标题:Multiple inheritance, base class pointers and sizeof

rel=“nofollow> 典型地表明点值如何通过投放而改变:

struct B1 { int x; };
struct B2 { int y; };
struct D  : B1, B2 { };

int main() {
   D d;
   cout << (B1*)&d << " " << (B2*)&d << " " << &d;
}

// Typical output:
// 0xbf814ab4 0xbf814ab8 0xbf814ab4

我想到的是,在<代码>B1无成员时,这种抵消可能不存在,因此我检查并 。 (至少就本案而言;不确定标准如何保障这种行为):

struct B1 { };
struct B2 { };
struct D  : B1, B2 { };

int main() {
   D d;
   cout << (B1*)&d << " " << (B2*)&d << " " << &d;
}

// Typical output:
// 0xbf6ad95b 0xbf6ad95b 0xbf6ad95b

但是,sizeof(T)不能成为0,因此,<>>>>>>>>>>>>>>>><1>>。

它告诉我,如果一名方案管理员假设<代码>(char*)(B2*)&d-(char*)(B1*)&d=(ptrdiff_t)sizeof(B1)

我的分析是否准确?

最佳回答

<代码>B1和B2的物体为<代码>d的分项。 <sizeof的操作者提供了有关完整物体大小的信息,而不是分目标。

《标准》允许但并不要求一个基类,不留记忆。 因此,关于另一个符合要求的执行情况,你可以在你的第二个例子中发现,次级目标在以后各有不同的地址。

1.8p5:除非它是一个禁区,否则,最衍生的物体应具有非零尺寸,并应当占有一个或多个贮存。 基地级次目标可能有零规模。 单微薄拷贝或标准传布的物体应持有毗连的储存。

1.8p6:除非物体为平地或基质类,其尺寸为零,该物体的地址是其占领的第一个星号。 两个不同的物体,既非轨道领域,也不属于零大小的基类次目标,应具有不同的地址。

唯一一种“安全”的点算法是:

  • On pointers to elements of the same array
  • The guarantee that the address of a subobject y of complete object x is between &x inclusive and &x+1 exclusive.

减去2个<代码> 避免* 查询机不知情。 您可能指的是<代码>reinterpret_cast<char*>或东西。 (另一个迹象是该法典非常危险)。)

5.7p4:为了这些操作者[集成<代码>+和-的目的,非通知物体的标识与一系列长度的第一部分的点子相同,其类型为标的类型。

5.7p6:如果对同一阵列的各个部分分两点,结果就是两个阵列各子的不同。 ...... 除非两点都提到同一阵列物体的要素,否则行为就没有界定。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签