From 95bfddc4329119535cf2fb47bef3c1b87f61e411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 6 Sep 2022 12:24:03 +0200 Subject: tis 6 sep 2022 12:24:03 CEST --- C++/Friend Operator.wiki | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 C++/Friend Operator.wiki (limited to 'C++/Friend Operator.wiki') diff --git a/C++/Friend Operator.wiki b/C++/Friend Operator.wiki new file mode 100644 index 0000000..a5e6730 --- /dev/null +++ b/C++/Friend Operator.wiki @@ -0,0 +1,30 @@ += Friend Operator = + +{{{c++ +#include + +class T { + int x; + +public: + T (int x); + + friend std::ostream& operator<<(std::ostream&, const T&); +}; + +T::T (int x) { + this->x = x; +} + + +std::ostream& operator<<(std::ostream& out, const T& t) { + out << "T {" << t.x << "}"; + return out; +} + +int main () { + T t(10); + + std::cout << t << std::endl; +} +}}} -- cgit v1.2.3