Re: If a class has a virtual destructor
a) A virtual destructor
b) A virtual function <ret>Name(int param1, int param2) const;
For point b, specifically I am interested in finding out how to detect
the virtuality of the call.
Try this one
#include "stdafx.h"
#include <iostream>
// class that has to be tested for
// virtual destructor
class CVirtualDtor
{
public:
virtual ~CVirtualDtor ()
{
std::cout << "virtual ~CVirtualDtor ()\n";
};
};
// class that has to be tested for
// virtual destructor
class CNoVirtualDtor
{
public:
~CNoVirtualDtor ()
{
std::cout << "~CNoVirtualDtor ()\n";
};
};
// this is the template function
//****************FUNCTION STARTS*******************//
template<typename T>
bool HasVirtualDtor ()
{
// a boolean return
static bool bRet = false;
// since CDerived has no member objects
// there is no harm if its destructor is skipped
// during this test
class CDerived : public T
{
public:
CDerived () {bRet = false;}
~CDerived () {bRet = true;}
};
// we shall call virtual mechanism to play here
// and the virtual d-tor will be revealed !
// assume success of new
T* oDerived = new CDerived ();
delete oDerived;
return bRet;
};
//****************FUNCTION ENDS*******************//
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "CNoVirtualDtor has a virtual d-tor ? : " <<
HasVirtualDtor<CNoVirtualDtor>() << std::endl;
std::cout << "CVirtualDtor has a virtual d-tor ? : " <<
HasVirtualDtor<CVirtualDtor>() << std::endl;
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]