Легенда:
новое сообщение
закрытая нитка
новое сообщение
в закрытой нитке
старое сообщение
|
- Напоминаю, что масса вопросов по функционированию форума снимается после прочтения его описания.
- Новичкам также крайне полезно ознакомиться с данным документом.
[C++ Bug] Bug report 24.07.01 12:16 Число просмотров: 901
Автор: XR <eXtremal Research> Статус: The Elderman
|
Попробовал вышло вот что :
> ============================= > >
> #define TRACE(x) printf(x)
>
> class Parrent
> {
> public:
> Parrent()
> {m_pParrent = new char[10];} // for fun
> virtual ~Parrent()
> {if (m_pParrent != NULL) delete [] m_pParrent;
> m_pParrent = NULL;}
> public:
> virtual void PrintMsg()
> {
> TRACE("This function called from class
> Parrent.\n");
> }
>
> protected:
> char* m_pParrent;
> };
>
> class Child: public Parrent
> {
> public:
> Child()
> {m_pChild = new char[30];} // just for fun, dummy
^^^^^^^^^^^^^^^^^^^
это скорее всего очепятка - добавляем char* m_pChild
> buffer
> virtual ~Child()
> {if (m_pChild != NULL) delete [] m_pChild; m_pChild =
> NULL;}
>
> public:
> virtual void PrintMsg()
> {
> TRACE("This function called from class Child.\n");
> }
>
> void Magic()
> {
> this->~Child();
^^^^^^^^^^^^^^^^^^^^^^^^^
Вот это КРУТО :)) надо запомнить :)
> new (this) Parrent;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
А вот на эту конструкцию мой компилер сказал
test3.cpp: In method `void Child::Magic()':
<internal>:39: too many arguments to function `void * operator new(unsigned int)'
test3.cpp:39: at this point in file
для справки
$g++ -v
gcc version 2.95.3 20010315 (release)
Я поправил на new(*this) Parrent
Это была фича или очепятка ?
> TRACE("Leave Magic.\n");
> }
> }
>
> int main()
> {
> Child child;
> Child* pChild = &child;
> Child& rChild = child;
>
> child.PrintMsg();
> pChild->PrintMsg();
> rChild.PrintMsg();
> TRACE("\n");
> child.Magic();
> TRACE("\n");
> child.PrintMsg();
> pChild->PrintMsg();
> rChild.PrintMsg();
>
> return 0;
> }
> ---
>
Результат:
This function called from class Child.
This function called from class Child.
This function called from class Child.
Leave Magic.
This function called from class Child.
This function called from class Parrent.
This function called from class Parrent.
---
Это видимо какие то фокусы с перегрузкой таблицы виртуальных функций ?
|
|
|