This blog reference to achieve any type of c ++ Any kind of , a little change. Original blog is to use a generic placeholder variable Holder object to hold the data, rather than the template class Any, Any class constructor is a template function to different types of data stored in the Holder. Any class implements this operation type erasure mainly used here is void*
a pointer, that is simply a combination of any of the data memory address and interpretation mode, void*
the pointer can point to any type of data stored pointer (i.e., memory address), the remaining reuse a string class variable to hold its type (i.e. how the data interpretation).
Using this embodiment setData
functions Any object class on a specific set of data using a getData
function to parse the data stored Any, both of which are generic functions:
class Any
{
public:
Any() :pData(NULL), type("null") {}
template<class ValueType>
Any(const ValueType& value) { setData(value); }
template<class ValueType>
void setData(const ValueType& value) { pData = (void*)(&value); type = typeid(value).name(); }
template<> void setData<Any>(const Any& value) { if (this != &value) { this->pData = value.pData; this->type = value.type; } }
template<class ValueType>
bool getData(ValueType& dst) { if (!isCompatible(dst))return false; dst = *(ValueType*)(pData); return true; }
template<class ValueType>
bool isCompatible(const ValueType& value) { return (string)(typeid(value).name()) == string(this->type); }
void* getAddress() { return pData; }
const char* getType() { return type; }
bool isNull() { return pData == NULL; }
template<class ValueType>
Any& operator=(const ValueType& value) { this->setData(value); return *this; }
template<class ValueType>
operator ValueType() { return *(ValueType*)(pData); }
template<class ValueType>
void display() { string t = (string)type; cout << "地址为:" << this << "的Any对象指向地址为:" << this->pData << "的" << this->type << "类型的数据:" << *(ValueType*)(pData) << endl; }
private:
void* pData;
const char* type;
};
Wherein constructors Any(const ValueType& value)
and =
operator overloading simply called setData
only provides a convenient use; comprising operator ValueType()
also a convenient way of providing the Any type to the other type of conversion; Any of the display()
functions just for testing. The test is as follows:
class Test
{
public:
int h = 4;
void display() { cout << "Test对象地址:" << this << ",h=" << h << endl; }
};
int main()
{
Any any, any1;
int a = 7, a1;
float b = 4.1, b1;
double c = 0.315, c1;
string s1 = "Hello", s2;
Test t, t1;
t.h = 5;
t1.h = 15;
any = a;
any.display<int>();
a1 = any;
cout << "a1=" << a1 << endl;
any = b;
any.display<float>();
b1 = any;
cout << "b1=" << b1 << endl;
any = c;
any.display<double>();
c1 = any;
cout << "c1=" << c1 << endl;
any = s1;
any.display<string>();
any.getData(s2);
//s2 = any;
cout << "s2=" << s2 << endl;
t.display();
any = t;
t1.display();
t1 = any;
t1.display();
any1 = any;
system("pause");
return 0;
}
Any class can be assigned can be seen as int
, float
, double
etc. basic types as well string
, and custom Test
type, a little fly in the ointment is that if you save Any object string
type can not directly through the =
assignment operator to string
the type of variable, there will be “operatr=”不明确
errors. I currently do not know what causes this. The following are the results:
the proposed use is:
- Any object to the assignment: direct use of
=
operators, such asany=b
- Any object assigned to the other variables: want to call
isCompatible
function to determine whether the same type, and then use thegetData
function; for some basic types ofint
,float
,double
can be used directly=
operator to assign to other types Any object, but the=
operator does not check the type of embodiment.
The method of the present embodiment is more similar to this blog achieve Any class .
Any additional boost class is defined in the library, you can use ready-made wheels.