#include <iostream>
#include <string>
using namespace std;

class CAnyType //: public CObject
{
public:
    CAnyType(){};
    //DECLARE_SERIAL(CAnyType)
    ~CAnyType(){};

protected:
    
    string DataType;
    // union还可以用来判断大端还是小端
    union{
        int myint;
        double mydouble;
        float myfloat;
        wchar_t mychar;} value;

public:
    CAnyType operator=(const int &in){value.myint=in;DataType="int";return *this;}
    CAnyType operator=(const wchar_t &in){value.mychar=in;DataType="char";return *this;}
    CAnyType operator=(const float &in){value.myfloat=in,DataType="float";return *this;}
    CAnyType operator=(const double &in){value.mydouble=in;DataType="double";return *this;}
    friend std::ostream & operator<<(std::ostream &os, const CAnyType& ca){
        os << ca.value.mydouble << std::endl;
        return os;
    };
};

int main()
{
    using namespace std;
    CAnyType test = CAnyType();
    test = 55;
    cout << test;
    return 0;
}




转载于:https://www.cnblogs.com/wangjiale1024/p/10289770.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐