Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

Commit a9b03e21 authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Encapsulation: typo and remove a detour that didn't add much to the topic considered.

parent 2c08a8a7
......@@ -828,7 +828,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Obviously, friendship should be used with parcimony... But it's not that much of a deal-breaker as it may seem:\n",
"Obviously, friendship should be used with parsimony... But it's not that much of a deal-breaker as it may seem:\n",
"\n",
"* The friendship must be defined in the class declaration. It means you can't use it to bypass a class encapsulation without modifying this code directly.\n",
"* The friendship is granted to a very specific function or class, and this class must be known when the class is defined. So an ill-intentioned user can't use the function prototype to sneak into your class private parts (in fact [forward declaration](../6-InRealEnvironment/2-FileStructure.ipynb#Forward-declaration) is an exception to current statement)."
......
......@@ -93,74 +93,6 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nitpick: `ClassName()` is very inefficient\n",
"\n",
"The method above is highly inefficient: the method reinstantiates a new string each time the method is called!\n",
"\n",
"A better implementation would be to use the `static` we already knew from C:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include <string>\n",
"\n",
"struct Class2\n",
"{\n",
" static const std::string& ClassName();\n",
" \n",
" Class2() = default;\n",
" \n",
"};"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"const std::string& Class2::ClassName()\n",
"{\n",
" static std::string ret = \"Class2\"; // on the very first call, the string is allocated\n",
" return ret; // on any calls, a reference to this instantiation is returned.\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
"{\n",
" std::cout << \"A static method may be called without any object instantiated: \" \n",
" << Class2::ClassName() << std::endl;\n",
" \n",
" Class2 obj;\n",
" \n",
" std::cout << \"But any object of the class may access it as if it was a regular method: \"\n",
" << obj.ClassName() << std::endl;\n",
" \n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll see this way of doing thing will solve an issue with data attributes we'll see immediately."
]
},
{
"cell_type": "markdown",
"metadata": {},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment