{"id":764,"date":"2015-10-27T14:34:26","date_gmt":"2015-10-27T14:34:26","guid":{"rendered":"http:\/\/coolt.ch\/notizen\/?p=764"},"modified":"2016-01-24T12:19:17","modified_gmt":"2016-01-24T12:19:17","slug":"c-geltungsbereich-von-variablen","status":"publish","type":"post","link":"https:\/\/coolt.ch\/notizen\/c-geltungsbereich-von-variablen\/","title":{"rendered":"C++ Geltungsbereich (Scope) von Variablen"},"content":{"rendered":"<blockquote><p>lokale Variabeln \u00fcberschreiben globale 8.4<\/p><\/blockquote>\n<p>Bjarne nennt in 8.4 unterschiedlichen <strong>Geltungsbereiche<\/strong> von Variablen<br \/>\n&#8211; global<br \/>\n&#8211; lokal<br \/>\n&#8211; in Klasse allein (siehe vektor unten)<br \/>\n&#8211; in Namensgebeit (z. B. struct, enum, &#8230;)<br \/>\n&#8211; in Aussagen (z. B. innerhalb eines for)<\/p>\n<p>Der Geltungsbereich hat mit dem <a href=\"http:\/\/coolt.ch\/notizen\/c-datenypen\/\">Datentyp<\/a> etwas zu tun, und auf welchem <a href=\"http:\/\/coolt.ch\/notizen\/speicherorte-die-segmente-im-ram\/\">RAM-Segment<\/a> der Code abgelegt wird.<\/p>\n<p><strong>extern<\/strong><\/p>\n<pre class=\"lang:sh decode:true \">extern int aktuelle_Version;<\/pre>\n<p>Die Variable wird nur deklariert und ist \u00fcberall sichtbar.<br \/>\nSie kann (theoretisch) in jedem File neu definiert und mit neuen Werten zugewiesen werden, das ist jedoch gef\u00e4hrlich. Besser ist es, nur <a href=\"http:\/\/coolt.ch\/notizen\/c-datenypen\/\">in 1 File die Variable zu definieren<\/a> und dann f\u00fcr alle anderen Files nur sichtbar zu machen.<\/p>\n<p><strong>Klasse<\/strong><\/p>\n<pre class=\"lang:sh decode:true\">class MyVector{\r\n.    \/\/ Klassenvariable. G\u00fcltigkeit in der Klasse \r\n.    <strong>vector &lt;int&gt; v<\/strong>      \r\n.\r\n.    public:\r\n.          \/\/ \u00f6ffentliche Funktion\r\n.          int largest(){\r\n.             int max = 0;\r\n.             for (int i = 0; i &lt; v.size(); i++) {\r\n.               max = max( x, abs(v[i]) );\r\n.             return max;  \r\n.          }\r\n.         \/\/ \u00f6ffentliche Variable\r\n.         int i;\r\n.\r\n}<strong>;<\/strong><\/pre>\n<p>Der <strong>Vektor v<\/strong> ist nur <em>in<\/em> der Klasse sichtbar. Ausserhalb exstiert er nicht und kann auch nicht angesprochen werden.<\/p>\n<p><strong>Argumente sind lokal<br \/>\n<\/strong><\/p>\n<pre class=\"lang:sh decode:true\">int funcition_b (<strong>int value_1<\/strong>){  \r\n.  \r\n.    \/\/\r\n}\r\n<\/pre>\n<p>Die Funktion ist global, aber der Parameter <span class=\"lang:sh decode:true crayon-inline \">value_1<\/span>\u00a0 ist lokal bez\u00fcglich der <span class=\"lang:sh decode:true crayon-inline\">function_b<\/span>\u00a0.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>constexpr &lt;function&gt;<\/strong><br \/>\nLokale Variablen liegen FIFO im Stack. Man kann nicht auf sie zugreifen (Bjarne 8.5.9). Soll eine Funktion w\u00e4hrend dem Kompilieren definiert werden, braucht sie das Schl\u00fcsselwort constexpr bei all ihren Variablen und f\u00fcr die Funktion.<\/p>\n<pre class=\"lang:sh decode:true\"><strong>constexpr<\/strong> int scale_x = 3;\r\n<strong>constexpr<\/strong> int scale_y = 2;<\/pre>\n<pre class=\"lang:sh decode:true\"><strong>constexpr<\/strong> Point scale( Point P){\r\n.\r\n.     return { scale_x * P.x, scale_y * P.y };\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>lokale Variabeln \u00fcberschreiben globale 8.4 Bjarne nennt in 8.4 unterschiedlichen Geltungsbereiche von Variablen &#8211; global &#8211; lokal &#8211; in Klasse allein (siehe vektor unten) &#8211; in Namensgebeit (z. B. struct, enum, &#8230;) &#8211; in Aussagen (z. B. innerhalb eines for) Der Geltungsbereich hat mit dem Datentyp etwas zu tun, und auf welchem RAM-Segment der Code &hellip; <a href=\"https:\/\/coolt.ch\/notizen\/c-geltungsbereich-von-variablen\/\" class=\"more-link\"><span class=\"screen-reader-text\">C++ Geltungsbereich (Scope) von Variablen<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[71,3],"_links":{"self":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/764"}],"collection":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":13,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":1179,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/764\/revisions\/1179"}],"wp:attachment":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}