D
dolphin2001
Unregistered / Unconfirmed
GUEST, unregistred user!
我以前是使用C++Build的,由于最近项目的原因开始使用DELPHI。现在碰到一个问题,以前在BCB中定义一个纯虚类,将其析构函数定义为虚函数,然后在此基类的基础上生成两个派生类,这样我若来调用其基类来实现多态的时候,在调用 delete 的方法的时候会自动去调用其派生类的析构函数。
比如:
class CTBase //纯虚类
{
public:
virtual void DoWork(void) = 0
virtual ~CTBase(void)
{
return
}
}
class CTWorkClass
{
private:
CTBase *FBase;
public:
void Work()
{
FBase->DoWork();
}
CTWorkClass(CTBase* InputBase)
{
FBase = InputBase;
}
~CTWorkClass(void)
{
delete FBase
//<---会去自动调用其派生类的析构函数
FBase = NULL;
}
};
class CTFirst : public CTBase //派生类一
{
private:
TStringList* FList
int ListLength
public:
void SetLength(const int value)
{
ListLength = value
}
void DoWork(void)
{
for (int i=ListLength
i>0
i--)
{
FList->Add(IntToStr(i));
}
Application->MessageBox("First Class","SysInfo",0);
} ;
CTFirst(void)
{
FList = new TStringList();
}
~CTFirst(void)
{
delete FList ;
FList = NULL;
}
};
class CTSecond : public CTBase //派生类二
{
public:
void DoWork(void)
{
Application->MessageBox("Second Class","SysInfo",0);
}
CTSecond(void)
{
return;
}
~CTSecond(void)
{
return;
}
};
但我在DELPHI,采用类似的方法,却不能得到在BCB执行的效果:(下面是的代码定义,实现部分已省略)
type
TBase_WorkClass = class
private
FVersion :string;
procedure SetVersion(value:string);
function GetVersion:string;
public
procedure DoWork
virtual;abstract;
procedure DoPrepare
virtual ;abstract;
public
destructor Destroy
virtual ;
published
property Version :string read GetVersion write SetVersion;
end;
type
TFirstClass = class(TBase_WorkClass)
private
FWorkList: TStringList;
FChainCount: Integer;
public
procedure DoWork
override;
procedure DoPrepare
override;
procedure SetChainLength(const ChainLength:Integer);
public
constructor Create;
destructor Destroy
override;
published
property Version;
end;
type
TSecondClass = class(TBase_WorkClass)
public
procedure DoWork
override;
procedure DoPrepare
override;
public
constructor Create;
destructor Destroy
override;
published
property Version;
end;
type
TWorkClass = class
private
FBaseClass:TBase_WorkClass;
public
procedure GotoWork;
public
constructor Create(BaseClass:TBase_WorkClass);
destructor Destroy
override
//跟踪其代码,没有去执行 FBaseClass 的相应派生类的析构函数???
end
比如:
class CTBase //纯虚类
{
public:
virtual void DoWork(void) = 0
virtual ~CTBase(void)
{
return
}
}
class CTWorkClass
{
private:
CTBase *FBase;
public:
void Work()
{
FBase->DoWork();
}
CTWorkClass(CTBase* InputBase)
{
FBase = InputBase;
}
~CTWorkClass(void)
{
delete FBase
//<---会去自动调用其派生类的析构函数
FBase = NULL;
}
};
class CTFirst : public CTBase //派生类一
{
private:
TStringList* FList
int ListLength
public:
void SetLength(const int value)
{
ListLength = value
}
void DoWork(void)
{
for (int i=ListLength
i>0
i--)
{
FList->Add(IntToStr(i));
}
Application->MessageBox("First Class","SysInfo",0);
} ;
CTFirst(void)
{
FList = new TStringList();
}
~CTFirst(void)
{
delete FList ;
FList = NULL;
}
};
class CTSecond : public CTBase //派生类二
{
public:
void DoWork(void)
{
Application->MessageBox("Second Class","SysInfo",0);
}
CTSecond(void)
{
return;
}
~CTSecond(void)
{
return;
}
};
但我在DELPHI,采用类似的方法,却不能得到在BCB执行的效果:(下面是的代码定义,实现部分已省略)
type
TBase_WorkClass = class
private
FVersion :string;
procedure SetVersion(value:string);
function GetVersion:string;
public
procedure DoWork
virtual;abstract;
procedure DoPrepare
virtual ;abstract;
public
destructor Destroy
virtual ;
published
property Version :string read GetVersion write SetVersion;
end;
type
TFirstClass = class(TBase_WorkClass)
private
FWorkList: TStringList;
FChainCount: Integer;
public
procedure DoWork
override;
procedure DoPrepare
override;
procedure SetChainLength(const ChainLength:Integer);
public
constructor Create;
destructor Destroy
override;
published
property Version;
end;
type
TSecondClass = class(TBase_WorkClass)
public
procedure DoWork
override;
procedure DoPrepare
override;
public
constructor Create;
destructor Destroy
override;
published
property Version;
end;
type
TWorkClass = class
private
FBaseClass:TBase_WorkClass;
public
procedure GotoWork;
public
constructor Create(BaseClass:TBase_WorkClass);
destructor Destroy
override
//跟踪其代码,没有去执行 FBaseClass 的相应派生类的析构函数???
end