什么是表示双值未被初始化的最好方法? [英] What is the best way to indicate that a double value has not been initialized?

查看:115
本文介绍了什么是表示双值未被初始化的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类CS,它表示3D中的坐标系统ie(x,y,z)

I have a class CS which is to represent the co-ordinate system in 3D i.e.(x, y, z)

class CS
{
  private:
        double x;
        double y;
        double z;
}

CS::CS()
{
   x = NULL;//this causes x = 0//i want the address of x to be 0x000000 & not x = 0
   y = NULL;
   z = NULL:
}



我想让用户可以创建CS (0,0,0)。
在构造函数中,我想初始化x,y, z为NULL。
这是为了区分用户定义的(0,0,0)&默认值。
我正在动态创建CS的对象,因此没有使用以下代码:

class CS
{
  private:
        double *x;
        double *y;
        double *z;
}

CS:CS()
{
    x = new double;
    x = NULL;
    //same for y & z
}

首先,我要 手动分配0x000000地址 到任何变量(int或double或char)而不使用指针。
任何建议?

Primarily, i want to manually assign 0x000000 address to any variable(int or double or char) without using pointers. any suggestions?

推荐答案

您不能将x,y和z的位置更改为NULL,因为位置将始终偏离CS对象。他们将永远存在。这不是 CS 有一个 x 像你有一辆汽车,它就像 CS 有一个 x 就像你有一个头。你不能有头。如果他们是整数,你必须使他们的指针(像你说你不想做),因为这将是告诉未初始化从初始化的唯一方法。但是, double 有一个很少使用的魔术值:

You can't change the positions of x,y,and z to be NULL, since there positions will always be offsets from the CS object. They will always exist. It's not that CS has an x like you have a car, it's like CS has an x like you have a head. You can't not have a head. If they were integers, you would have to make them pointers (like you said you didn't want to do), because that would be the only way to tell uninitialized from initialized. However, doubles have a magic value that is rarely used:

CS:CS()
: x(std::numeric_limits<double>::quiet_NaN())
: y(std::numeric_limits<double>::quiet_NaN())
: z(std::numeric_limits<double>::quiet_NaN())
{ }

用户可能不会将x,y和z设置为(非数字)。

Users probably won't be setting x, y, and z to (NOT A NUMBER) intentially.

这篇关于什么是表示双值未被初始化的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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