CPB Mailing List
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Deriving from TObject
In Delphi, all classes are ultimately derived from the TObject class.
i.e.
TMyClass = class
{ stuff }
end; { My Class }
is equivalent to
TMyClass = class(TObject)
{ stuff }
end; { My Class }
In C++, this is doesn't hold. That is:
class TMyClass {
// stuff
}; // TMyClass
is NOT equivalent to
class TMyClass: public TObject {
// stuff
}; // TMyClass
Why am I bringing this point up, you ask? Because I've always used TList
in Delphi to hold a list of TObjects so I've always assumed it returned
TObjects and thus, I've recently made a point of deriving my C++ classes
from TObject (second C++ declaration above) so that, should I need them in
a list, I could use the tried and true TList class to hold them (Ok, so I'm
a VCL junky). But upon utilizing TList for the first time in C++ in (at
least in C++ code), I got a compiler error when I tried to type cast a
return value as follows:
TMyItem* __fastcall TMyItemList::GetItem(int Index)
{
return dynamic_cast<TMyItem*>(FList->Items[Index]);
} // Get Item
Upon closer inspection of the VCL source, I realized that TList->Items
returns (void *) (a.k.a. the Pointer type for fellow Delphians).
The only way I could compile the GetItem function was as follows:
TMyItem* __fastcall TMyItemList::GetItem(int Index)
{
return (TMyItem*)(FList->Items[Index]);
} // Get Item
It looks like the added security of dynamic_cast is cast out the Windows
:-) in this scenario so I ask, is there *any* advantages to deriving
classes from TObject? Are there *any* disadvantages?
Michael
W Komornicki's Home Page |
Main Index |
Thread Index