M
Mark
I'd like to make a class that can be implicitly cast to doubles, or
strings, or whatever (which I don't think is possible in C++).
Anyways, I came up with the following code:
struct var {
var() : dbl(0) {}
union {
double dbl;
char str[32];
};
};
double var(const var &v) {
return v.dbl;
}
Which, interestingly enough compiles without any errors. The idea
would be that I could then pass a var object to any function that
accepts doubles, like tan() for example, without having to pass in
var.dbl or anything (it would just know which variable to use via the
function provided).
However, this code seems to be completely unusable. It doesn't seem
to be possible to declare a var object, and since I can't create a var
object, I can't pass a var object to the function other... why would C+
+ even allow me write this? Shouldn't the var function and var struct
conflict?
strings, or whatever (which I don't think is possible in C++).
Anyways, I came up with the following code:
struct var {
var() : dbl(0) {}
union {
double dbl;
char str[32];
};
};
double var(const var &v) {
return v.dbl;
}
Which, interestingly enough compiles without any errors. The idea
would be that I could then pass a var object to any function that
accepts doubles, like tan() for example, without having to pass in
var.dbl or anything (it would just know which variable to use via the
function provided).
However, this code seems to be completely unusable. It doesn't seem
to be possible to declare a var object, and since I can't create a var
object, I can't pass a var object to the function other... why would C+
+ even allow me write this? Shouldn't the var function and var struct
conflict?