diff --git a/2-ObjectProgramming/1-Introduction.ipynb b/2-ObjectProgramming/1-Introduction.ipynb index 735c557830272cbc5e856fe97e872df2d6419c4f..23da4eb32cbd75c1c23775f9c757aa937530da0a 100644 --- a/2-ObjectProgramming/1-Introduction.ipynb +++ b/2-ObjectProgramming/1-Introduction.ipynb @@ -43,7 +43,7 @@ "double norm(double v_x, double v_y, double v_z) \n", "{ \n", " return std::sqrt( v_x * v_x + v_y * v_y + v_z * v_z ); \n", - "};\n", + "}\n", "\n", "{\n", " double v1_x, v1_y, v1_z;\n", @@ -158,6 +158,41 @@ "Let's also highlight the `.` syntax which allows to access the attributes of an object (e.g `v1.x`).\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The semicolon at the end of a `struct`\n", + "This comes historically from the C, where a `struct` is considered as a type declaration and it allows to do:\n", + "\n", + "**Xeus-cling issue:** Here cling doesn't manage to compile it but it is accepted rightfully by a full-fledged compiler (see for instance [@Coliru](http://coliru.stacked-crooked.com/a/3b77606ea8082485)):" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "struct Vector\n", + "{\n", + " double x;\n", + " double y;\n", + " double z; \n", + "} v1; // Here the struct is declared and at the same time an object v1 is created\n", + "\n", + "v1.x = 1.;\n", + "v1.y = 5.;\n", + "v1.z = -2.;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the reason why a semicolon is always required at the end of a `struct` or a `class` declaration." + ] + }, { "cell_type": "markdown", "metadata": {},