在C ++中使用原始数据类型 [英] Inherting Primitive Data Types in C++

查看:83
本文介绍了在C ++中使用原始数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中定义一个无符号的double数据类型。你能告诉我这样做的最好方法吗?我考虑使用双成员变量定义一个类并覆盖所有可能的运算符(很多:()但这看起来非常混乱。有这样一种巧妙的方法吗?



最初的问题是定义一个保持方差并使其类型安全的变量。我不想断言每次变量被污染时该值都不会消极。我希望它自动完成。我想考虑到C ++的属性实现(在代码项目中找到),但实现一个简单的Conditional Double似乎太多了。

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.

推荐答案

沿着Alain Rists的想法:

Along the lines of Alain Rists idea:
#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;





现在你甚至可以重复使用这个实施方案:)



问候

Espen Harlinn



Now you can even reuse the implmentation :)

Regards
Espen Harlinn




之前的答案表明你的方法不适合你的问题。使用私有成员来控制您的值,例如:

Hi,
The previous answers show that your approach is improper to your problem. Use a private member to control your value, for instance:
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;
};



使用运算符long double(),无需覆盖所有可能的运营商

欢呼,

AR


With operator long double () there is no need to override all possible operators.
cheers,
AR


不幸的是,你没有继承原始类型而是写一个围绕它的包装。

我只面对Ada语言中原始类型的真正继承,这种方法很棒! (有不同的声明类型和子类型;对于类型,隐式转换不会编译。)



回到C ++和你的包装器,你真的需要结合所有的算术运算包装器之间以及包装器和原始数字类型之间。 (那是你提到的凌乱吗?没有别的方法)。问题是:增加的功能是什么?这只是约束,我使用了不同的方法。



前一段时间我开发了元数据引擎(MDE)架构,我们开发了团队实施(在C ++),以及使用该引擎的商业软件产品。其中一个目标是用它们的元模型替换许多无聊的数据类,它们以自动方式创建例程数据结构。引擎的一小部分是meta²级数据域的支持,域指定了包含约束的基本类型(例如Multiplicity,它是基数的约束)。



您可以看到,当您包装双数据类型时,您将其值(数据,元数据)与元数据(例如约束)混淆。如果您创建通用数据结构(例如,无限数量的字符串,整数,布尔值,枚举和浮点属性),您可以远离此。结构内容和规则由meta¹数据(属性名称,描述,对域的引用,以及返回到点,约束)控制。每个数据结构元数据实例都保留对其meta¹数据类型的引用。通用数据结构操作的通用规则使用约束。



-SA
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


这篇关于在C ++中使用原始数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆