我们有一个基类 A,B 类从 A 派生。现在 B 类是使用placement new 运算符分配的。作为我的代码的一部分,我在 A 类中添加了 C 类
现在placement new 只使用了B 类sizeof 的malloc,不考虑C 类。C 类构造函数在B 类构造函数内部调用。(这是我的嫌疑人)
我想知道那是否正确。还是我们在放置新分配中添加 C 类的大小?如果我们把内存加起来,我们如何调用类 C 的构造函数,它会再次分配内存?
card.h {
class Card
{
private:
char hwModel; // HW/FW model and rev are stored in the
// database to detect change of card type
char hwRev;
char serialNum[EM_CARD_SERIAL_NUM_SIZE];
char fwModel;
char fwRev;
>>>>
public:
class IssuResource *ptrIssuResrc;
void *operator new(size_t size, void *objP) {size = size; return objP;};
// overload the new operator
void operator delete(void *objP) { objP = objP;};
// overload the delete operator
Card();
}
chopperCard.cpp
// Allocate Memory and Create Card Object
/* sa_ignore */
buf = (UINT32 *)malloc(sizeof(CardChop));
if (buf == NULL) {
emLogError(EM_INTERNAL_ERROR, __FUNCTION__,
"exit: failed to allocate %d bytes for card object\n",
sizeof(CardChop)
);
exit(1);
}
/* sa_ignore */
card = new (buf) CardChop(spa_issu_restart); --> placement new
}
Constructor for choppercard:
{
CardChop::CardChop (boolean is_issu_set) : Card()
{
issu_inprogress = is_issu_set;
if (is_issu_set) {
ISSUTRACE(4,"Issu state is SET\n");
dev_np37xx_update_issu_state(is_issu_set);
dev_set_issu_state(is_issu_set);
dev_dm_issu_state(is_issu_set);
dev_tigris_issu_state(is_issu_set);
dev_evros_issu_state(is_issu_set);
dev_evros1_issu_state(is_issu_set);
>>>
vtwin->fvt->set_gp_stat(vtwin, VTWIN_GP_STAT_APP_TRACE1,
CHOP_BG_INIT_FAIL);
// init Bourget device
initDevice((dev_object_t *)bourget);
vtwin->fvt->set_gp_stat(vtwin, VTWIN_GP_STAT_APP_TRACE1, CHOP_BG_INIT_AFTER);
ptrIssuResrc = new IssuResource();
}