Here is the sample code that throws compile time error message if you try to derive from the non-drivable class.
// DerivedFromParticularClass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <assert.h>
#include <iostream>
using namespace std;
class Cloneable
{
};
class DerivedFromCloneable:public Cloneable
{
};
class NotDerivedFromCloneable
{
};
template <typename D,typename B>
class IsDervivedFrom {
public:
//method:1
class No {};
class Yes { No arr[3]; };
static Yes test(B*){ }
static No test(...) {}
///
static void Constraints(D* pd)
{
B* pb = pd;
}
IsDervivedFrom()
{
cout<<sizeof(test(static_cast<D*>(0)));
void (*p)(D*) = Constraints;
}
enum { Is= sizeof(test( static_cast<D*>(0) )) == sizeof(Yes) };
};
template <typename T >
class Sample :public IsDervivedFrom <T,Cloneable>
{
};
int _tmain(int argc, _TCHAR* argv[])
{
Sample <DerivedFromCloneable> p_obj;
Sample <NotDerivedFromCloneable> f_obj; //ERROR
IsDervivedFrom<DerivedFromCloneable,Cloneable> Y;
assert(Y.Is);
return 0;
}
// DerivedFromParticularClass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <assert.h>
#include <iostream>
using namespace std;
class Cloneable
{
};
class DerivedFromCloneable:public Cloneable
{
};
class NotDerivedFromCloneable
{
};
template <typename D,typename B>
class IsDervivedFrom {
public:
//method:1
class No {};
class Yes { No arr[3]; };
static Yes test(B*){ }
static No test(...) {}
///
static void Constraints(D* pd)
{
B* pb = pd;
}
IsDervivedFrom()
{
cout<<sizeof(test(static_cast<D*>(0)));
void (*p)(D*) = Constraints;
}
enum { Is= sizeof(test( static_cast<D*>(0) )) == sizeof(Yes) };
};
template <typename T >
class Sample :public IsDervivedFrom <T,Cloneable>
{
};
int _tmain(int argc, _TCHAR* argv[])
{
Sample <DerivedFromCloneable> p_obj;
Sample <NotDerivedFromCloneable> f_obj; //ERROR
IsDervivedFrom<DerivedFromCloneable,Cloneable> Y;
assert(Y.Is);
return 0;
}
No comments:
Post a Comment