## [[C++]]は[[値渡し]] [[C++]]は普通に引数を渡すと[[値渡し]]。[[仮引数]]にいかなる操作をしても[[実引数]]には影響がない。 ```cpp #include <cstdio> #include <cstdlib> #include <iostream> #include <string> struct Coordinate { int x; int y; }; void destroyArgString(std::string x) { x = "wriiiiiiiiiiiiiiiii"; } void destroyArgStruct(Coordinate s) { s = { 65535, 65535 }; } void destroyArgStructReference(Coordinate s) { s.x = 65535; s.y = 65535; } int main() { std::string x = "init"; destroyArgString(x); std::cout << x << std::endl; // init Coordinate s1 = { 0, 0 }; destroyArgStruct(s1); std::cout << "x=" << s1.x << ", y=" << s1.y << std::endl; // x=0, y=0 Coordinate s2 = { 0, 0 }; destroyArgStruct(s2); std::cout << "x=" << s2.x << ", y=" << s2.y << std::endl; // x=0, y=0 } ``` <button class="playground"><a href="https://wandbox.org/permlink/2HjCK2usYdVN9Gr2">Playground</a></button> ## [[C++]]は[[参照渡し]]もサポート [[C++]]は[[参照渡し]]もサポートしている。[[仮引数]]に操作をすると[[実引数]]に影響が出る。 ```cpp #include <cstdio> #include <cstdlib> #include <iostream> #include <string> struct Coordinate { int x; int y; }; void destroyArgString(std::string& x) { x = "wriiiiiiiiiiiiiiiii"; } void destroyArgStruct(Coordinate& s) { s = { 65535, 65535 }; } void destroyArgStructReference(Coordinate& s) { s.x = 65535; s.y = 65535; } int main() { std::string x = "init"; destroyArgString(x); std::cout << x << std::endl; // wriiiiiiiiiiiiiiiii Coordinate s1 = { 0, 0 }; destroyArgStruct(s1); std::cout << "x=" << s1.x << ", y=" << s1.y << std::endl; // x=65535, y=65535 Coordinate s2 = { 0, 0 }; destroyArgStruct(s2); std::cout << "x=" << s2.x << ", y=" << s2.y << std::endl; // x=65535, y=65535 } ``` <button class="playground"><a href="https://wandbox.org/permlink/8EPQDUEvSpRQzy7X">Playground</a></button>