How to define a data structure to satisfy this?

A easy questions, maybe, for you guys.

I want to define a class, which will have two members, for example, vaporPressureStatus and vaporPressure


1
2
3
4
5
6
7
enum vpStatus_t {nonesense, unknown, known, saturated};

class pore_t {
public:
    vpStatus_t vpStatus;
    double vaporPressure;
};


when vpStatus is nonsense and unknown, the vaporPressure should not have a value; and if I calculate out a value for vaporPressure, the vpStatus can be set as known.

I am wondering if there is any set, pair or other structure can hold this two members together, so that when I change one's value, the other guy will also change accordingly.

Thanks, if my questions is not clear, please let me know.
closed account (D80DSL3A)
Now this is what getter and setter functions are for, when the values of data members are interdependent.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum vpStatus_t {nonesense, unknown, known, saturated};

class pore_t {
private:// do not allow these values to be manipulated directly
    vpStatus_t vpStatus;
    double vaporPressure;
public:// use this interface to do it
double setVP( double VP )
{
    if( VP > 0.0 )
   {
        vaporPressure = VP;
        vpStatus = known;
    }
}
// and so on as further details demand.    
};
Last edited on
Thanks, so, I need to define some good class member functions to synchronize them
If it is just 'may or may not be set', std::optional<> (C++14) or boost::optional<>
http://www.cplusplus.com/forum/general/110828/#msg605076

If there are more states, perhaps emulate a numeric type with piggybacked state. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdexcept>
#include <iostream>

struct vapour_pressure
{
    enum status_t { nonsense, unknown, known, saturated } ;

    vapour_pressure() = default ;
    vapour_pressure( double v ) { *this = v ; }

    vapour_pressure& operator= ( double v )
    {
        value = v ;
        if( value < 0 ) status = nonsense ;
        // else if ...
        else status = known ;
        return *this ;
    }

    operator double() const
    {
        if( status == nonsense ) throw std::domain_error("nonsense") ;
        if( status == unknown ) throw std::domain_error("unknown") ;

        return value ;
    }

    bool valid() const { return status == known || status == saturated ; }

    operator bool () const { return valid() && value ; }
    bool operator! () const { return !bool(*this) ; }

    // etc.

    private:
        double value ;
        status_t status = unknown ;
        // ...

    friend std::ostream& operator<< ( std::ostream& stm, const vapour_pressure& vp )
    {
        if( vp.status == vapour_pressure::unknown ) return stm << "unknown" ;
        else if( vp.status == vapour_pressure::nonsense ) return stm << "nonsense" ;
        else if( vp.status == vapour_pressure::saturated ) return stm << "saturated" ;
        else return stm << vp.value ;
    }
};

int main()
{
    vapour_pressure vp ;
    std::cout << "vapour pressure is: " << vp << '\n' ;
    if(vp) { double val = vp ; /* ... */ }

    vp = 23.4 ;
    std::cout << "vapour pressure is: " << vp << '\n' ;
    if( vp.valid() ) { double psi = double(vp) * 14.5038 ; /* ... */ }

    vp = -3 ;
    std::cout << "vapour pressure is: " << vp << '\n' ;
}

http://ideone.com/J2DlQk
Topic archived. No new replies allowed.