나는이 트릭으로 파생 된 기본 클래스를 제거하고 다시 만들 수 있습니까?런타임 선택베이스 -ctor로 복사 불가능한 기본 클래스를 초기화하는 방법
class base: noncopyable
{
base(); //ctor with none param
base(int x); //ctor with one param
base(int x, int y); //ctor with two param
virtual ~base();
}
struct params
{
int x;
int y;
enum
{
typeNoneParam, //neither x nor y is defined
typeOneParam, //only x is defined
typeTwoParam //x and y both are defined
}typeParam;
}
class Derived
{
Derived(params p); //construct base class conditionally by p.typeParam
}
Derived::Derived(params p)
:base() //default typeNoneParam
{
//typeNoneParam need not do special process
if (p.typeParam == params::typeOneParam)
{
base::~base(); //delete the default-typeNoneParam creation by base-dtor
base(p.x); //recreate the new base with one-param base-ctor
}
if (p.typeParam == params::typeOneParam)
{
base::~base(); //delete the default-typeNoneParam creation by base-dtor
base(p.x, p.y); //recreate the new base with two-param base-ctor
}
}
클래스 파생 및 기본 cannnot에 대한 모든 선언은 변경할 수 없으며 struct params도 cannnot입니다.
파생 클래스의 구현 만 변경 허용됩니다.
누구나 아이디어를 줄 수 있습니까? 그리고 다른 모든 더 부드러운 구현은이 시나리오를 만족시킵니다 (동적으로 선택하는 기본 ctor로 초기화 불가능한 기본 클래스).
게시 된 코드는'Derived '의 초기화 목록에서'base' 생성자를 호출 할 때 컴파일되지 않지만'Derived'는 실제로'base'를 상속받지 않으며'base '유형의 멤버 변수도 포함하지 않습니다 '. –