{"id":2686,"date":"2017-02-06T16:21:19","date_gmt":"2017-02-06T15:21:19","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2686"},"modified":"2017-02-25T08:51:11","modified_gmt":"2017-02-25T07:51:11","slug":"c-guns-call-by-reference-return-by-auto","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2686","title":{"rendered":"C++ Guns - call by reference; return by auto"},"content":{"rendered":"<p>Es gibt viele Arten Daten in eine Funktion hinein zu stecken und auch wieder heraus zu bekommen. Hier liste ich die Vor- und Nachteile auf.<br \/>\nHinein ist einfach:<br \/>\n<strong><br \/>\nCall by reference:<\/strong><br \/>\nSofern es sich um stink normale Daten handelt, ist das die bevorzugte Methode. Nicht stink normale sind z.B. Funktionen\/-Objekte. <\/p>\n<pre><code>\r\nvoid callByRef(const Point3D& point);\r\n<\/code><\/pre>\n<p>Eine Referenze belegt dabei genauso viel Platz wie ein Pointer, also 8byte. Hat man also nur ein int\/real zu kopieren, lohnt es sich nicht. Bei so kleinen Objekten ist call by value genauso gut. <\/p>\n<p><strong>Call by value:<\/strong><br \/>\nBevorzugt f\u00fcr kleine Objekte oder wenn explizit eine Kopie gebraucht wird. Auch f\u00fcr Funktionsobjekte, Lambdas, Iteratoren etc. Da call by value immer eine Kopie erzeugt, sollt es nicht f\u00fcr gro\u00dfe Objekte verwendet werden.<\/p>\n<pre><code>\r\ntemplate<class Iterator, class Function, class Rand>\r\nvoid callByValue(const int x, int float, Iterator iter, Function pow, Rand rand);\r\n<\/code><\/pre>\n<p><strong>Call by pointer:<\/strong><br \/>\nNee braucht man nicht. Alle sinnvolle F\u00e4lle sind schon durch reference und value abgedeckt. Ausserdem, schonmal versucht ein const Pointer auf ein const Objekt zu machen? H\u00e4sslich.<\/p>\n<pre><code>\r\nvoid callByPointer(Data const * const data) {\r\n *data = x; \/\/ BUMM\r\n data++; \/\/ BUMM\r\n}\r\n<\/code><\/pre>\n<p>Heraus ist schwer:<br \/>\n<strong>Return by reference:<\/strong><br \/>\nSo etwas macht nur Sinn, wenn die Funktion eine Memberfunktion einer Klasse ist. z.B: std::vector::at(). Das Objekt, auf das die Referenz zeigt, muss ja noch weiter existieren, nachdem die Funktion wieder verlassen wurde. Aber normale Variablen in Funktionen existieren nur bis zum Funktionsende. <\/p>\n<p><strong>Return by value:<\/strong><br \/>\nIch nehme an, dass ist der normale Fall. F\u00fcr einfache Typen (int, real) ist das auch kein Problem. F\u00fcr komplexe Datentypen mit move Konstructor ist es auch kein Problem. z.B.<\/p>\n<pre><code>\r\nauto returnByValue() { return std::vector<int> x;}\r\nstd::vector<x> = returnByValue();\r\n<\/code><\/pre>\n<p>Ansonsten kann noch die Compiler Optimierung zuschlagen und einem retten. Aber seit dem man moven kann ist das alles kein Ding mehr.<\/p>\n<p><strong>Return by auto:<\/strong><br \/>\nMehrere Variablen zur\u00fcck geben ist kein Problem.<\/p>\n<pre><code>\r\nauto returnByAuto() { return make_pair(x, true); }\r\nauto [x, ok] = returnByAuto();\r\n<\/code><\/pre>\n<p>Structured Bindings nennt sich das.<\/p>\n<p><strong>Return by pointer:<\/strong><br \/>\nKomm, spar's dir.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Es gibt viele Arten Daten in eine Funktion hinein zu stecken und auch wieder heraus zu bekommen. Hier liste ich die Vor- und Nachteile auf. Hinein ist einfach: Call by reference: Sofern es sich um stink normale Daten handelt, ist das die bevorzugte Methode. Nicht stink normale sind z.B. Funktionen\/-Objekte. void callByRef(const Point3D&#038; point); Eine [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-2686","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2686"}],"version-history":[{"count":7,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions"}],"predecessor-version":[{"id":2719,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions\/2719"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2686"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}