-
Unsigned/signed classes?
Is it possible to design a user-defined data type that the designer can also dictate its behavior if declared signed or unsigned (by the user)?
Is it even possible to say something like:
and have the option of:
Code:
unsigned myClass objName;
???
-
-
Quote:
Originally Posted by
matsp
use templates.
--
Mats
I want to create a 512 -bit data type, this is why I ask...
-
>unsigned myClass objName;
Not really, but you can get close with template specialization:
Code:
enum signedness { SIGNED, UNSIGNED };
template <signedness T>
class my_class {};
template<>
class my_class<SIGNED> {
public:
my_class() { std::cout<<"signed my_class\n"; }
};
template<>
class my_class<UNSIGNED> {
public:
my_class() { std::cout<<"unsigned my_class\n"; }
};
int main()
{
my_class<SIGNED> foo;
my_class<UNSIGNED> bar;
}
-
thanx, that should help a bit, no pun intended.