With all this PHP programming I've been doing lately, I've been thinking about writing a class that acts like it's variables. For those who are completely unfamiliar with PHP, you can assign essentially any type to a variable. For example:
PHP Code:
$somevar = 123;
$somevar = "a string!";
$somevar = new array();
$somevar[0] = 456;
$somevar[1] = "another string";
This is a very neat magic trick and I thought it would be fun to implement something like that myself in C++. I am completely unaware if there is already something like it already exists for C++ (even if it does I'd like to do it myself), but if there is I'd like to know about it.
Basically why I'm telling you all this is that I'm in the middle of implementing it and all is going well. You can assign any primitive type to an object and cast from any type to another. You can even have an array of mixed types; here's a line from my testing (It's really neat stuff):
Code:
Morph arr[COUNT] = { "null", 1, "Three3", 4444, 'X', 3.14159f, UINT_MAX, str, wchar_t(65535) };
Anyway, as I'm getting deeper into it things are getting more complicated. For example, if you want to assign an array you can use a constructor that also accepts the number of elements and a copy is made on the heap for the object. Now I'm thinking that you should also be able to assign an object to a pointer (just pointing, not copying). What issues come to mind when you contemplate building such a class? I still haven't given much thought to how it will handle being assigned user-defined types and such. Right now the class is so filled with templates and explicit specializations that it's sort of depressing to look at. There are some complications and problems I don't understand, but I'm saving that fun for later.
If you have any thoughts on how such a class should be implemented or on some things that one shouldn't forget (special cases, etc), please share them.