crossplatform.ru

Здравствуйте, гость ( Вход | Регистрация )

> Расположение в памяти для Множественного и Виртуального наследования, Memory Layout for Multiple and Virtual Inheritance
Andrew Selivanov
  опции профиля:
сообщение 12.10.2007, 13:03
Сообщение #1


Участник
**

Группа: Участник
Сообщений: 249
Регистрация: 9.10.2007
Из: Москва
Пользователь №: 3

Спасибо сказали: 15 раз(а)




Репутация:   6  


Расположение в памяти для Множественного и Виртуального наследования
Memory Layout for Multiple and Virtual Inheritance
(By Edsko de Vries, January 2006)

http://www.phpcompiler.org/doc/virtualinheritance.html

Warning. This article is rather technical and assumes a good knowledge of C++ and some assembly language.

In this article we explain the object layout implemented by gcc for multiple and virtual inheritance. Although in an ideal world C++ programmers should not need to know these details of the compiler internals, unfortunately the way multiple (and especially virtual) inheritance is implemented has various non-obvious consequences for writing C++ code (in particular, for downcasting pointers, using pointers to pointers, and the invocation order of constructors for virtual bases). If you understand how multiple inheritance is implemented, you will be able anticipate these consequences and deal with them in your code. Also, it is useful to understand the cost of using virtual inheritance if you care about efficiency. Finally, it is interesting :-)

Multiple Inheritance

First we consider the relatively simple case of (non-virtual) multiple inheritance. Consider the following C++ class hierarchy.
class Top
{
public:
   int a;
};

class Left : public Top
{
public:
   int b;
};

class Right : public Top
{
public:
   int c;
};

class Bottom : public Left, public Right
{
public:
   int d;
};

Using a UML diagram, we can represent this hierarchy as

Note that Top is inherited from twice (this is known as repeated inheritance in Eiffel). This means that an object bottom of type Bottom will have two attributes called a (accessed as bottom.Left::a and bottom.Right::a).

How are Left, Right and Bottom laid out in memory? We show the simplest case first. Left and Right have the following structure:
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение

Сообщений в этой теме


Быстрый ответОтветить в данную темуНачать новую тему
Теги
Нет тегов для показа


1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0


RSS Рейтинг@Mail.ru Текстовая версия Сейчас: 8.6.2025, 9:25