Can a template object change its type?

I have the following class

template <typename T> class Item {

public:

    T item;
    Item () : item( T() ) {}
    Item (T arg) { this->item = arg;}

    operator T () const {
        return item;
    }

};

Now I want to write an assignment operator that also changes the type of an object. Is it possible? I was looking for him, but nothing came of it (which, incidentally, makes me think that maybe I'm a little crazy).

To make this clear, let's say I have the following two objects:

Item <int> intItem = 3;
Item <double> doubleItem = 3.4;

I want to write

intItem = doubleItem;

And after that, I want the intItem type to be Item<double>.

If I only needed the “classic” assignment operator, it would work fine if inside my class I would have something like

Item<int>& operator= (const Item<double> & var) {
    this->item = var.item;
    return *this;
}

A double value will be rounded, but it will work. Oh, and the intItem type would remainItem<int>

P.S.: , . Item "- " .

, , , .

+4
3

, .

-. :

class Item
{

    Item()
    : data(NULL) {}

    template <typename T>
    Item(T d)
    {
        data = new Data<T>(d);
    }

    Item(const Item& other)
    : data(NULL)
    {
        if (other.data != NULL)
        {
            data = other.data->clone();
        }
    }

    ~Item()
    {
        delete data;
    }

    const Item& operator = (const Item& other)
    {
        if (this != &other)
        {
            delete data;a
            data = NULL;
            if (other.data != NULL)
            {
                data = other.data->clone();
            }
        }
        return *this;
    }

    template <typename T>
    operator T () const 
    {
        Data<T>* d = dynamic_cast<Data<T>*>(data);
        if (d != NULL)
        {
            return d->data;
        }
        else
        {
            throw std::bad_cast("");
        }
    }

private:
    struct DataBase
    {
        virtual ~DataBase() {}

        virtual DataBase* clone() = 0;
    };

    template <typanme T>
    struct Data
    {
        Data(T d)
        : data(d) {}

        virtual DataBase* clone() 
        {
            return new Data<T>(*this);
        }

        T data;
    };

    DataBase* data;

};

Any. , boost::any, , .

, , - :

class Item
{
    enum Type
    {
        NONE,
        INT,
        DOUBLE
    };


    Item()
    : type(NONE) {}

    Item(int value)
    : type(INT)
    {
        intValue = value;
    }

    Item(double value)
    : type(DOUBLE)
    {
        doubleValue = value;
    }

    Item(const Item& other)
    {
        type = other.type;
        switch (type)
        {
            case INT:
                intValue = other.intValue;
                break;
            case DOUBLE:
                doubleValue = other.doubleValue;
                break;
        }
    }

    ~Item()
    {
        // delete any pointer types
    }

    const Item& operator = (const Item& other)
    {
        if (this != &other)
        {
            type = other.type;
            switch (type)
            {
                case INT:
                    intValue = other.intValue;
                    break;
                case DOUBLE:
                    doubleValue = other.doubleValue;
                    break;
            }
        }
        return *this;
    }

    operator int () const 
    {
        switch (type)
        {
            case NONE:
                return 0;
            case INT:
                return intValue;
                break;
            case DOUBLE:
                return static_cast<int>(doubleValue);
                break;
        }
    }

    operator double () const 
    {
        switch (type)
        {
            case NONE:
                return 0;
            case INT:
                return static_cast<double>(intValue);
                break;
            case DOUBLE:
                return doubleValue;
                break;
        }
    }

private:
    Type 
    union {
        int intValue;
        double doubleValue;
    };
};
+1

, , .

, , int, float. , float, .

+6

And after that, I want the intItem type to be Item.

This is unrealistic because it intItemis of type Item<int>at compile time, not at run time.

+2
source

All Articles