D
David Rasmussen
I want to have an unsigned integer-like thing, that for now only allows
the bitwise operators. I have defined these operators and their
self-assigning cousins.
This works, but I would like to know what the most effecient way of
doing this is. Preferably, there should be no overhead compared to a
read unsigned long int. No extra temporaries, full inlining etc. The
same code should be generated.
The only difference between an unsigned long int and my class should be
that it is it's own separate type and that I control what operations and
conversions are possible/automatic.
class MyClass
{
public:
MyClass() {}
explicit MyClass(unsigned long int uli)
{
bb = uli;
}
MyClass& operator &=(const MyClass& rhs) { bb &= rhs.bb; return
*this; }
MyClass& operator |=(const MyClass& rhs) { bb |= rhs.bb; return
*this; }
MyClass& operator ^=(const MyClass& rhs) { bb ^= rhs.bb; return
*this; }
MyClass operator &(const MyClass& rhs) { return MyClass(bb & rhs.bb); }
MyClass operator |(const MyClass& rhs) { return MyClass(bb | rhs.bb); }
MyClass operator ^(const MyClass& rhs) { return MyClass(bb ^ rhs.bb); }
private:
unsigned long int bb;
};
/David
the bitwise operators. I have defined these operators and their
self-assigning cousins.
This works, but I would like to know what the most effecient way of
doing this is. Preferably, there should be no overhead compared to a
read unsigned long int. No extra temporaries, full inlining etc. The
same code should be generated.
The only difference between an unsigned long int and my class should be
that it is it's own separate type and that I control what operations and
conversions are possible/automatic.
class MyClass
{
public:
MyClass() {}
explicit MyClass(unsigned long int uli)
{
bb = uli;
}
MyClass& operator &=(const MyClass& rhs) { bb &= rhs.bb; return
*this; }
MyClass& operator |=(const MyClass& rhs) { bb |= rhs.bb; return
*this; }
MyClass& operator ^=(const MyClass& rhs) { bb ^= rhs.bb; return
*this; }
MyClass operator &(const MyClass& rhs) { return MyClass(bb & rhs.bb); }
MyClass operator |(const MyClass& rhs) { return MyClass(bb | rhs.bb); }
MyClass operator ^(const MyClass& rhs) { return MyClass(bb ^ rhs.bb); }
private:
unsigned long int bb;
};
/David