#include <iostream> #include <string> void printValue(std::string& y) // type changed to std::string& { std::cout << y << '\n'; } // y is destroyed here int main() { std::string x { "Hello, world!" }; printValue(x); // x is now passed by reference into reference parameter y (inexpensive) return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter