Memory Layout difference between nested classes and multiple inheritance in C++?

前端 未结 4 1906
盖世英雄少女心
盖世英雄少女心 2020-12-19 18:59

I am trying to understand how COM specifies the layout of its objects so that a client that wants to use a COM object knows how to do it.

I\'ve read that a COM obje

4条回答
  •  無奈伤痛
    2020-12-19 19:30

    I don't believe a single COM interface can have multiple inheritance, but a class may implement multiple interfaces through multiple inheritance. So the multiple inheritance layout is irrelevant - each interface will have a unique layout, and it is up to the compiler to provide a pointer to the proper layout.

    For single inheritance the compiler will place the parent class definitions at the front, followed by the child class. This is defined by the standard for data elements, but again this is irrelevant since interfaces don't have data. The standard says nothing about the existance or layout of vtables, but for polymorphism to work it has to be laid out the same way - parent first, child second.

    You will discover a surprising fact if you implement multiple interfaces through multiple inheritance. As you cast a pointer to your class object from one interface to another, the address will change! This is because the different interfaces (vtables) must match the interface declaration, so there must be different layouts. These layouts are all contained within the same object, but the compiler does pointer manipulations when casting to get it to the proper subset.

提交回复
热议问题