tags:

views:

77

answers:

2

This is kinda like my earlier question: http://stackoverflow.com/questions/2451175/c-vector3-type-wall

Except, now, I want to do this to a builtin rather then a user created type.

So I want a type "Length" that behaves just like float -- except I'm going to make it's constructor explicit, so I have to explicitly construct Length objects (rather than have random conversions flying around).

Basically, I'm going into the type-a-lot camp.

+4  A: 

Like suggested in a comment over at your other question you can use units from boost. This should be explicit and still manageable.

honk
I *suppose* I should up-vote, thief... :P
GMan
Thanks GMan. I am never sure what can be done about questions which have very closely related spin-off questions: neither exact duplicates nor bad enough for a downvote, but they still make SO less comprehensible.
honk
@GMan: I was really looking for answers that are "open box" rather than "use boost"
anon
@anon: Eh, like I said in my comment, a usable system isn't very trivial. Your best bet would probably look at how boost does it and see if you can strip the important stuff out to keep.
GMan
+1  A: 

It sounds like you want to wrap a float primitive in your own class. Here's an example to get you started:

class Length
{
protected:
    float value_;
public:
    Length(float value) : value_(value) { }
    static Length operator +(Length a, Length b) { return Length(a.value_ + b.value_); }
    static Length operator -(Length a, Length b) { return Length(a.value_ - b.value_); }
    static Length operator *(Length a, Length b) { return Length(a.value_ * b.value_); }
    static Length operator /(Length a, Length b) { return Length(a.value_ / b.value_); }
};

But, using the boost Units library is a much better choice in the long run...

Inverse