From 49877ddc4a53b38300662cce0ff4bc380ce918cd Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Thu, 6 May 2021 16:15:59 +0200 Subject: [PATCH] Remove semicolon at the end of function declaration, and explains why it is required to add a semicolon at the end of a struct --- 2-ObjectProgramming/1-Introduction.ipynb | 37 +++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/2-ObjectProgramming/1-Introduction.ipynb b/2-ObjectProgramming/1-Introduction.ipynb index 735c557..23da4eb 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": {}, -- GitLab