Что полный список действий выполняется размещением, новым в C++?

Я не уверен, что это решило проблему, но предупреждение и проблема исчезли, как только я сделал следующее:

Выберите CollectionViewController в Интерфейсном Разработчике и установите размер для произвольной формы и сделайте высоту больше, больше достаточно, чтобы показать все ваши ячейки шаблона. (Плюс: возможно, обновить взгляды). Это решило проблему для меня.

7
задан Community 23 May 2017 в 12:25
поделиться

2 ответа

Размещение новое делает все, что делает обычный new , за исключением выделения памяти.

Я думаю, что вы, по сути, добились того, что происходит, с некоторыми небольшими пояснениями:

  • очевидно, что конструктор самого класса также вызывается
  • Указатели vtable инициализируются как часть вызовов конструктора, а не отдельно. Следствием этого является то, что частично сконструированный объект (подумайте об исключениях, созданных в конструкторе) имеет свою vtable, установленную до точки, к которой перешло построение.

Порядок построения / инициализации следующий:

  1. виртуальные базовые классы в порядке объявления
  2. невиртуальные базовые классы в порядке объявления
  3. члены класса в порядке объявления
  4. сам конструктор класса
6
ответ дан 7 December 2019 в 01:25
поделиться

set vtable pointer accordingly

This part is almost completely implementation-defined. Your compiler might not use vtables. There may be multiple vtable pointers, or one or more pointers to things which are not vtables. Multiple inheritance is always entertaining, as are virtual base classes. This metadata is not guaranteed to be copyable with memcpy to another object, so the pointer(s) needn't be absolute. There could be offsets in there which are relative to the object pointer itself.

IIRC what commonly happens is that the base class constructor is called, then the vtable pointer is set to the base class, then the first derived class constructor is called, etc. This is in order to satisfy the requirements in the specification concerning what happens when a virtual function is called in a constructor. As far as I remember, there is no "list of actions" in the standard, just a defined initialisation order.

So it is not possible to generalise what an implementation does, especially since what you have is not an implementation of the C++ standard. If it cuts corners by leaving out "new", presumably with good reason because it thinks you shouldn't be using it on the target platform, then who knows what other rules of the language it ignores. If it were possible to mock up "new" with a malloc and a bit of pointer-pushing, then why on earth does the compiler not just implement new? I think you need to ask questions tagged with your specific compiler and platform, so that any experts on your compiler can respond.

4
ответ дан 7 December 2019 в 01:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: