0

関数を使用して 2 レベルの派生クラスを削除し、そのハンドルを null に設定したいと考えています。コードの一部が役立ちます:

ref class bob
{
};

ref class bill : public bob
{
};

ref class jack : public bill
{
};

void DeleteX( bob ^% x )
{
  if( x != nullptr )
  {
    delete x;
    x = nullptr;
  }
}

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
  bill^ one = gcnew jack();
  DeleteX(one);
  System::Diagnostics::Trace::Assert(one == nullptr); //Failed
  return 0;
}

宣言と関数の引数に同じ型を使用すると、機能します。しかし、宣言には中間型を使用し、関数の引数には上位型を使用したいと考えています。どうすればこれを行うことができますか?

これは私が最終的に使用するソリューションです:

template<class T>
void DeleteX( T ^% x )
{
  if( x != nullptr )
  {
    delete x;
    x = nullptr;
  }
}
4

1 に答える 1

1

それは私のために働く...

ref class bob
{
};

ref class bill : public bob
{
};

void DeleteX( bob ^% x )
{
  if( x != nullptr )
  {
    delete x;
    x = nullptr;
  }
}

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    bob^ one = gcnew bill();
    DeleteX(one);
    System::Diagnostics::Trace::Assert(one == nullptr); //did not trigger
于 2009-09-29T14:22:11.520 に答える