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

前端 未结 9 1929
萌比男神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 06:55

    Why can't you simply do this:

    class CS
    {
    public:
        // Constructs a CS initialized to 0, 0, 0
        CS() : x(0), y(0), z(0), is_initialized(false) {}
    
        // User defined values
        CS(double newX, double newY, double newZ) : x(newX), y(newY), z(newZ), is_initialized(true) {}
    
    private:
        double x;
        double y;
        double z;
    
        // If you need to know that this was initialized a certain way, you could use this suggestion from the comments:
        bool is_initialized;
    }
    

提交回复
热议问题