What is the best way to indicate that a double value has not been initialized?

前端 未结 9 1923
萌比男神i
萌比男神i 2020-12-22 06:26

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         


        
9条回答
  •  伪装坚强ぢ
    2020-12-22 07:04

    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::quiet_NaN())
    : y(std::numeric_limits::quiet_NaN())
    : z(std::numeric_limits::quiet_NaN())
    { }
    

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

提交回复
热议问题