B
balor
Sometimes it is useful to assign class names to types that are
primitives even if they add no more functionality, at the very least to
be able to determine which constants apply to a type:
typedef int Address;
void test(Address address) { ... }
const Address ADDRESS1 = 0xff, ADDRESS2 = 0x3f;
Of course, this method is type unsafe since any int can be passed in
but to the reader it is at least clear that ADDRESS1 and ADDRESS2 are
associated with argument 1 in function test(). The alternative, more
typesafe way is to:
class Address {
public:
operator=(int address) ...
operator int() ...
.. all other operators
static const Address ADDRESS1, ADDRESS2;
private:
Address(int address) ...
};
This creates an enumeration that can behave as an integer. If the items
in the enum don't need actual values, then one can just compare the
address of the items for identity. To be even more safe, replace the
casting operator with a method. Unfortunately, this method works much
better in Java than in C++ as the constants must be defined in a
separate file, a large list of functions must be overloaded, and
remembering to compare by address is painful. This implementation is
also too large for a realtime system with memory constraints. GCC is
also nice enough to print a warning that it has no public constructors.
So my question is, I like the method 1 for creating a type but I have
no good place to put the constants for it and was wondering if anyone
had any ideas. What I'm looking for is something like:
class Address : public int {
// constants
};
Of course this doesn't work. I thought maybe something like this would
work:
typedef int Address;
namespace Address { // constants }
But unfortunately it doesn't. Any ideas on how to accomplish this?
primitives even if they add no more functionality, at the very least to
be able to determine which constants apply to a type:
typedef int Address;
void test(Address address) { ... }
const Address ADDRESS1 = 0xff, ADDRESS2 = 0x3f;
Of course, this method is type unsafe since any int can be passed in
but to the reader it is at least clear that ADDRESS1 and ADDRESS2 are
associated with argument 1 in function test(). The alternative, more
typesafe way is to:
class Address {
public:
operator=(int address) ...
operator int() ...
.. all other operators
static const Address ADDRESS1, ADDRESS2;
private:
Address(int address) ...
};
This creates an enumeration that can behave as an integer. If the items
in the enum don't need actual values, then one can just compare the
address of the items for identity. To be even more safe, replace the
casting operator with a method. Unfortunately, this method works much
better in Java than in C++ as the constants must be defined in a
separate file, a large list of functions must be overloaded, and
remembering to compare by address is painful. This implementation is
also too large for a realtime system with memory constraints. GCC is
also nice enough to print a warning that it has no public constructors.
So my question is, I like the method 1 for creating a type but I have
no good place to put the constants for it and was wondering if anyone
had any ideas. What I'm looking for is something like:
class Address : public int {
// constants
};
Of course this doesn't work. I thought maybe something like this would
work:
typedef int Address;
namespace Address { // constants }
But unfortunately it doesn't. Any ideas on how to accomplish this?