64,636
社区成员
发帖
与我相关
我的任务
分享
struct TypeA{
char a[4];
int ToInt()const;
string ToString()const;
};
struct TypeB{
char b[4];
int ToInt()const;
string ToString()const;
};
class Type{
public:
Type(char);//create TypeA
Type(char,char,char,char);//create TypeB
int ToInt()const{
if(flag)
return type.type_a.ToInt();
else
return type.type_b.ToInt();
}
string ToString()const;
private:
bool flags;
union {
TypeA type_a;
TypeB type_b;
}type;
};
class TypeBase
{
public:
virtual ~TypeBase(){}
virtual int ToInt()const=0;
virtual string ToString()const=0;
};
class TypeA:public TypeBase
{
public:
TypeA(char);
int ToInt()const;
string ToString()const;
};
class TypeB:public TypeBase
{
public:
TypeB(char,char,char,char);
int ToInt()const;
string ToString()const;
};
class Type
{
public:
Type(char c):type_ptr(new TypeA(c)){}
Type(char c,char s,char d,char n):type_ptr(new TypeB(c,s,d,n)){}
int ToInt()const{return type_ptr->ToInt();}
string ToString()const{return type_ptr->ToString();}
private:
TypeBase *type_ptr;
};