Click here to Skip to main content
15,886,581 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I want to define a unsigned double data type in C++. Can you please tell me the best way of doing this? I considered defining a class with a double member variable and overriding all possible operators (a lot :( ) but that seems very messy. Is there a neat way of doing this?

The original problem is to define a variable that holds variance and make it type safe. I don't want to be asserting that the value doesn't get negative, each time the variable is tainted. I want it done automatically. I considered Property implementation (found on Code Project) for C++, but it seems too much to do for implementing a simple Conditional Double.
Posted
Updated 9-Jun-17 1:13am
v2
Comments
Sergey Alexandrovich Kryukov 25-Feb-11 20:54pm    
Very good question, my 5. The approach makes sense (if only it is not overkill for the purpose).
--SA

Along the lines of Alain Rists idea:
C++
#include <cmath>

template<typename T>
class Primitive
{
private:
    T _var;
public:
    Primitive(const T& var)
        : _var(std::abs(var))
    {}

    operator T ()
    {
        return _var;
    }

    Primitive& operator = (const T& var)
    {
        _var = std::abs(var);
        return *this;
    }
};

typedef Primitive<double> DoublePrimitve;
typedef Primitive<long> LongPrimitve;
typedef Primitive<__int64> LongLongPrimitve;
typedef Primitive<long double> LongDoublePrimitve;
typedef Primitive<float> FloatPrimitve;


Now you can even reuse the implmentation :)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Feb-11 10:51am    
My 5. So good in C++ one can use templates like that.
--SA
Espen Harlinn 26-Feb-11 11:17am    
Thanks SAKryukov - I guess you did mean "can't" and not "can" :)
Sergey Alexandrovich Kryukov 26-Feb-11 18:01pm    
No, I meant "C++ can", now modified, thanks to your note.
I could not rightfully say C# cannot, because this is .NET to blame.
And this is due to lack of inheritance of primitive types as I already argued.
--SA
vsurendhaaren 28-Feb-11 8:22am    
Espen, Thanks for showing it is extendable to any primitive.
Espen Harlinn 28-Feb-11 13:49pm    
Glad you liked it :)
Hi,
The previous answers show that your approach is improper to your problem. Use a private member to control your value, for instance:
C++
struct Variance
{
    Variance(long double var) : _var(std::abs(var))
    {}
    operator long double ()
    {
        return _var;
    }
    Variance& operator = (const long double& var)
    {
        _var = std::abs(var);
        return *this;
    }
private:
    long double _var;
};

With operator long double () there is no need to override all possible operators.
cheers,
AR
 
Share this answer
 
Comments
Espen Harlinn 26-Feb-11 5:26am    
My 5, nice and simple
vsurendhaaren 28-Feb-11 8:21am    
Thanks a lot Alain. This works perfect.
Unfortunately, you're not inheriting a primitive type but writing a wrapper around it.
I only faced with true inheritance of primitive types in Ada language, and that approach is brilliant! (There are different declarations type and subtype; with types, implicit conversion does not compile.)

Back to C++ and your wrapper, you really need to combine all arithmetic operations between wrappers and also between wrappers and primitive numeric type. (Is that you mentioned by messy? There is no other way). Which comes to the question: what's the added functionality? Is this is just constraints, I used different approach.

A while ago I developed architecture of Meta-data Engine (MDE), we developed implementation with the team (in C++), as well as commercial software product using the engine. One of the goal was to replace many boring data classes with meta-models of them, which created the routine data structures authomatically. One small part of the Engine was the support of data Domains on meta² level, and Domains specified primitive types including constrains (such as Multiplicity which is a constraint for Cardinality).

You see, when you wrap your double data type, you mix-up its valued (data, meta⁰-data) with meta¹-data, such as constraint. You could go away from this if you create universal data structures (for example, unlimited number of string, integer, boolean, enumeration and floating-point properties). The structure content and rules are controlled by meta¹-data (property names, descriptions, references to domains, and, back to the point, constraints). Every data structure meta⁰-data instance keep the reference to its meta¹-data type. Universal rule for operation on universal data structures make use of constraints.

—SA
 
Share this answer
 
Comments
Espen Harlinn 26-Feb-11 5:25am    
Good reply, my 5
Sergey Alexandrovich Kryukov 26-Feb-11 10:52am    
Thank you, Espen.
This is rather a hint; very hard to explain the approach in brief.
--SA
If you want to have a separate class to represent a non-negative double that you can use anywhere you want, you'll have to create a class, override those operators, and watch out for casting. I'm not sure there is any simpler way to do it.

If this variance value is itself a member of only one or two other classes and not used independently like a class or struct, then I would consider just using a double (or other appropriate numeric type) and controlling access to it through functions that validate the value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900