What is the best way to indicate that a double val

2020-02-15 08:12发布

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:
}

I want that the user can create a CS (0, 0, 0). In the constructor i want to initialise the address of x, y & z to NULL. this is to differentiate between the user defined (0, 0, 0) & the default value. I am creating the objects of CS dynamically, so there is no point in using the following code:

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

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

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

标签: c++ null
9条回答
淡お忘
2楼-- · 2020-02-15 08:56

You have several options:

  1. Use pointers.
  2. Use a boolean flag alongside each variable indicating whether the variable has been set.
  3. If the range of allowable values is limited, you could use a special value to stand for "not set". For double, a not-a-number is often a natural candidate. For int and char it's often more tricky to pick a good value.

None of these options is indisputably better than the other two as they involve different tradeoffs. Take your pick.

查看更多
3楼-- · 2020-02-15 08:56

One way to get the semantics of what you want would be to have the datatype of the coordinates be a type that carries with it a value indicating whether it has been assigned. Something like this.

template<typename T>
class CoordinateValue {
   public:
       CoordinateValue() : uninitialized(true), val(0) {}
       CoordinateValue(T x) : uninitialized(false), val(x) {}
       void setVal(T x) {val = x; uninitialized= false}
       // Trivial getters
    private:
       T val;
       bool uninitialized;
};

I'd prefer something like this over cuter methods unless memory is really scarce for some reason.

If the coordinates are either all default or all set, then you can have a single flag rather than a coordinate datatype that includes the flag.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-15 08:58

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

That's not what you want. What you want is the ability to detect whether a variable has been set or not.

Others have suggested things like using a specific floating-point value to detect the uninitialized state, but I suggest employing Boost.Optional. Consider:

class CS
{
  private:
    boost::optional<double> x;
    boost::optional<double> y;
    boost::optional<double> z;
}

boost::optional either stores the type you give to the template parameter or it stores nothing. You can test the difference with a simple boolean test:

if(x)
{
  //Has data
}
else
{
  //Has not been initialized
}

The downside is that accessing the data is a bit more complex:

x = 5.0; //Initialize the value. x now has data.
y = 4.0 * x; //Fails. x is not a double; it is an optional<double>.
y = 4.0 * (*x); //Compiles, but only works at runtime if x has a value.
查看更多
何必那么认真
5楼-- · 2020-02-15 09:04

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;
}
查看更多
Explosion°爆炸
6楼-- · 2020-02-15 09:09

If I understand correctly, you want to be able to tell the difference between an invalid, default constructed CS and a valid one with values (0.0, 0.0, 0.0). This is exactly what boost::optional http://www.boost.org/doc/libs/1_47_0/libs/optional/doc/html/index.html is for.

查看更多
【Aperson】
7楼-- · 2020-02-15 09:10

I want that the user can create a CS (0, 0, 0). In the constructor i want to initialise the address of x, y & z to NULL. this is to differentiate between the user defined (0, 0, 0) & the default value. I am creating the objects of CS dynamically, so there is no point in using the following code:

This is the problem. Firstly, default value? What default value? Why should there be a default value? That's wrong. And secondly, it's fundamentally impossible for you to change the address of any variable.

What you want cannot be done and even if it could, it would be a horrendously bad idea.

查看更多
登录 后发表回答