| 4 | #include <iostream> |
| 5 | |
| 6 | class Box |
| 7 | { |
| 8 | public: |
| 9 | // Constructors |
| 10 | Box() = default; |
| 11 | Box(double length, double width, double height); |
| 12 | |
| 13 | double volume() const; // Const function to calculate the volume of a box |
| 14 | |
| 15 | // Non-const overloads (return references to dimension variable) |
| 16 | double& length() { std::cout << "non-const overload called\n"; return m_length; }; |
| 17 | double& width() { std::cout << "non-const overload called\n"; return m_width; }; |
| 18 | double& height() { std::cout << "non-const overload called\n"; return m_height; }; |
| 19 | |
| 20 | // Const overloads (return references to const variables) |
| 21 | const double& length() const { std::cout << "const overload called\n"; return m_length; }; |
| 22 | const double& width() const { std::cout << "const overload called\n"; return m_width; }; |
| 23 | const double& height() const { std::cout << "const overload called\n"; return m_height; }; |
| 24 | |
| 25 | // Attempt to return non-const references to member variables from const functions |
| 26 | // double& length() const { return m_length; }; // This must not be allowed to compile! |
| 27 | // double& width() const { return m_width; }; |
| 28 | // double& height() const { return m_height; }; |
| 29 | |
| 30 | private: |
| 31 | double m_length{1.0}; |
| 32 | double m_width {1.0}; |
| 33 | double m_height{1.0}; |
| 34 | }; |
| 35 | |
| 36 | #endif |
nothing calls this directly
no outgoing calls
no test coverage detected