perplexed by this use of the conditional operator

Hello all. I am trying to learn C++ and have been lurking here for a month or so, and finally decided to join and sign up.

I have been reading Ivor Horton's "Beginning Visual C++ 2010". And I just can't seem to figure out how to understand the meaning of the syntax on a certain line of code using the conditional operator.

The line of code is

T theMax = m_Free ? m_Values[0] : 0;

You can see the full program below. But I just can't figure out why the line was written that way. And what exactly it is doing.

Specifically, how and why he is using the conditional operator. I have never seen it used this way before. In other instances where I have seen the conditional operator used a specific condition is described before the question mark ? Like the example on http://www.cplusplus.com/doc/tutorial/operators/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// conditional operator

#include <iostream>
using namespace std;

int main ()
{
  int a,b,c;

  a=2;
  b=7;
  c = (a>b) ? a : b;

  cout << c;

  return 0;
}



In the of the line of code I am stuck on, a new variable (theMax) is declared and having its value set to the member function m_Free. How can this be a condition? And then the two selections on the right side of the conditional operator are an array position and 0. Are those the two choices, and what are they based on? and what will happen to them? I just don't understand how it relates to the left side of the line?

Any help would be greatly appreciated.

OK so here is the full code for the program with the line that I do not understand the line I don't get is 102:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Ex8_10.cpp
// Using a class template
#include <iostream>
using std::cout;
using std::endl;

class CBox                             // Class definition at global scope
{
  public:
    // Constructor definition
    CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0): m_Height(hv)
    {
      m_Length = lv > wv? lv: wv;      // Ensure that 
      m_Width = wv < lv? wv: lv;       // length >= width
    }

    // Function to calculate the volume of a box
    double Volume() const
    {
      return m_Length*m_Width*m_Height;
    }

    // Operator function for 'greater than' which
    // compares volumes of CBox objects.
    int CBox::operator>(const CBox& aBox) const
    {
      return this->Volume() > aBox.Volume();
    }

    // Function to compare a CBox object with a constant
    int operator>(const double& value) const
    {
      return Volume() > value;
    }

    // Function to add two CBox objects
    CBox operator+(const CBox& aBox) const
    {
      // New object has larger length & width, and sum of heights
      return CBox(m_Length > aBox.m_Length? m_Length:aBox.m_Length,
                  m_Width > aBox.m_Width? m_Width:aBox.m_Width,
                  m_Height + aBox.m_Height);
    }

    // Function to show the dimensions of a box
    void ShowBox() const
    {
      cout << m_Length << " " 
           << m_Width  << " " 
           << m_Height << endl;
    }

  private:
    double m_Length;                   // Length of a box in inches
    double m_Width;                    // Width of a box in inches
    double m_Height;                   // Height of a box in inches
};
// CSamples class template definition 
template <class T> class CSamples 
{
  public:
    // Constructors
    CSamples(const T values[], int count);
    CSamples(const T& value);
    CSamples(){ m_Free = 0; }

    bool Add(const T& value);          // Insert a value
    T Max() const;                     // Calculate maximum

  private:
    T m_Values[100];                   // Array to store samples
    int m_Free;                        // Index of free location in m_Values
};

// Constructor template definition to accept an array of samples
template<class T> CSamples<T>::CSamples(const T values[], int count)
{
  m_Free = count < 100? count:100;     // Don't exceed the array
  for(int i = 0; i < m_Free; i++)
    m_Values[i] = values[i];           // Store count number of samples
}

// Constructor to accept a single sample
template<class T> CSamples<T>::CSamples(const T& value)
{
  m_Values[0] = value;                 // Store the sample
  m_Free = 1;                          // Next is free
}

// Function to add a sample
template<class T> bool CSamples<T>::Add(const T& value)
{
  bool OK = m_Free < 100;              // Indicates there is a free place
  if(OK)
    m_Values[m_Free++] = value;        // OK true, so store the value
  return OK;
}

// Function to obtain maximum sample
template<class T> T CSamples<T>::Max() const
{
  T theMax = m_Free ? m_Values[0] : 0; // Set first sample or 0 as maximum
  for(int i = 1; i < m_Free; i++)      // Check all the samples
    if(m_Values[i] > theMax)
      theMax = m_Values[i];            // Store any larger sample
  return theMax;
}

int main()
{
  CBox boxes[] = {                          // Create an array of boxes
                   CBox(8.0, 5.0, 2.0),     // Initialize the boxes...
                   CBox(5.0, 4.0, 6.0),
                   CBox(4.0, 3.0, 3.0)
                 };

  // Create the CSamples object to hold CBox objects
  CSamples<CBox> myBoxes(boxes, sizeof boxes / sizeof CBox);

  CBox maxBox = myBoxes.Max();              // Get the biggest box
  cout << endl                              // and output its volume
       << "The biggest box has a volume of "
       << maxBox.Volume() << endl;
  return 0;
}




Correct me if im wrong but in the line in question, should it be

T theMax == m_Free ? m_Values[0] : 0

The way it is up there, hes using the assignment operator as an equality operator
Thanks for the quick reply. It was not written the way you suggested.
The way it was written in the book, there is only one = in the line.
And the program compiles and runs just fine.
In the of the line of code I am stuck on, a new variable (theMax) is declared and having its value set to the member function m_Free.


m_Free is not a member function. It is an int.

T theMax = m_Free ? m_Values[0] : 0;

This says "if m_Free evaluates to true (which any int does if it has any value other than zero, so it's saying if m_Free is not zero), set theMax equal to m_Values[0]. Otherwise, set theMax equal to 0."

Here is some code doing the same thing:

1
2
3
4
5
6
7
8
if (m_free != 0)
{
  theMax = m_Values[0];
}
else
{
  theMax = 0;
}
Last edited on
Moschops

Thanks that was an excellent answer. And solved my question. I guess the thing I didn't understand was that by simply writing the variable name before the conditional operator you are in fact writing a statement that says if this variable name evaluates to true.

I tested it in this simple program and I can see from the output that you are 100% correct. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;


int main(void)
{
    
    int a (0);
    int b = a ? 999:666;
    cout << b << endl;        // this showed me 666 because a = 0
    
    a = 1;
    b = a ? 999:666;
    cout << b << endl;        //this showed me 999 because a != 0
    

  return 0;
}

Topic archived. No new replies allowed.