_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 8](/notebooks/3-Operators/1b-TP.ipynb)
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 9](/notebooks/3-Operators/1b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<spanclass="tocSkip"></span></h1>
<divclass="toc"><ulclass="toc-item"><li><span><ahref="#Introduction"data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><ahref="#EXERCICE-31:-replace-PowerOfTwoApprox::DoubleValue-by-an-operator-double"data-toc-modified-id="EXERCICE-31:-replace-PowerOfTwoApprox::DoubleValue-by-an-operator-double-2">EXERCICE 31: replace <code>PowerOfTwoApprox::DoubleValue</code> by an <code>operator double</code></a></span></li><li><span><ahref="#EXERCICE-32:-replace-PowerOfTwoApprox::Multiply-by-operator-*"data-toc-modified-id="EXERCICE-32:-replace-PowerOfTwoApprox::Multiply-by-operator-*-3">EXERCICE 32: replace <code>PowerOfTwoApprox::Multiply</code> by <code>operator *</code></a></span></li><li><span><ahref="#EXERCICE-33:-commutativity"data-toc-modified-id="EXERCICE-33:-commutativity-4">EXERCICE 33: commutativity</a></span></li><li><span><ahref="#EXERCICE-34:-operator[]"data-toc-modified-id="EXERCICE-34:-operator[]-5">EXERCICE 34: <code>operator[]</code></a></span></li></ul></div>
<divclass="toc"><ulclass="toc-item"><li><span><ahref="#Introduction"data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><ahref="#EXERCICE-26:-replace-PowerOfTwoApprox::AsDouble-by-an-operator-double"data-toc-modified-id="EXERCICE-26:-replace-PowerOfTwoApprox::AsDouble-by-an-operator-double-2">EXERCICE 26: replace <code>PowerOfTwoApprox::AsDouble</code> by an <code>operator double</code></a></span></li><li><span><ahref="#EXERCICE-27:-replace-PowerOfTwoApprox::Multiply-by-operator-*"data-toc-modified-id="EXERCICE-27:-replace-PowerOfTwoApprox::Multiply-by-operator-*-3">EXERCICE 27: replace <code>PowerOfTwoApprox::Multiply</code> by <code>operator *</code></a></span></li><li><span><ahref="#EXERCICE-28:-operator[]"data-toc-modified-id="EXERCICE-28:-operator[]-4">EXERCICE 28: <code>operator[]</code></a></span></li></ul></div>
%% Cell type:markdown id: tags:
### Introduction
[This notebook](/notebooks/TP/HowTo.ipynb) explains very briefly your options to run the TP.
%% Cell type:markdown id: tags:
### EXERCICE 31: replace `PowerOfTwoApprox::DoubleValue` by an `operator double`
### EXERCICE 26: replace `PowerOfTwoApprox::AsDouble` by an `operator double`
The arguments should remain the same.
%% Cell type:markdown id: tags:
### EXERCICE 32: replace `PowerOfTwoApprox::Multiply` by `operator *`
### EXERCICE 27: replace `PowerOfTwoApprox::Multiply` by `operator *`
Arguments remain unchanged.
%% Cell type:markdown id: tags:
### EXERCICE 33: commutativity
In `TestDisplaySum::Display()`, you should have the line
Please ensure the operator is commutative: we want the line
````
int approx = approx1 * coefficient1 + approx2 * coefficient2;
int approx = approximation1 * coefficient1 + coefficient2 * approximation2;
````
Try the following one instead:
````
int approx = coefficient1 * approx1 + coefficient2 * approx2;
````
You should get at least a warning doing so... (at least with the CMake build I provide that activates `-Wconversion` warning - more on that [later](/notebooks/6-InRealEnvironment/3-Compilers.ipynb)...)
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
What happens in fact is that `operator*` is not commutative: it expects as first argument an object and as second argument an integer.
So why does it work nonetheless?
`operator*(int, PowerOfTwoApprox)` is not defined... but addition between an integer and a double is obviously possible, AND you've just defined before an implicit conversion to `double`.
You should of course heed the warning and fix your code:
* Make `operator double` explicit. Except in very specific cases, I advise you to always make such conversion operators explicit to avoid unintended side effects as the one we've just got.
* If commutativity is important for you, make `operator*` a free function instead of a method and provide both ordering of arguments.
The solution file implements both these modifications.
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_