From 4806f0ecfb2aa10f274fc1158cc8152cc266f092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Sat, 7 Sep 2019 00:00:00 +0000 Subject: [PATCH] c++ template class example --- README.adoc | 2 ++ userland/cpp/template.cpp | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 userland/cpp/template.cpp diff --git a/README.adoc b/README.adoc index 5110641..d89df7f 100644 --- a/README.adoc +++ b/README.adoc @@ -12982,6 +12982,8 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik * link:userland/cpp/empty.cpp[] * link:userland/cpp/hello.cpp[] +* templates +** link:userland/cpp/template.cpp[] [[cpp-multithreading]] ==== C++ multithreading diff --git a/userland/cpp/template.cpp b/userland/cpp/template.cpp new file mode 100644 index 0000000..7f7c038 --- /dev/null +++ b/userland/cpp/template.cpp @@ -0,0 +1,17 @@ +// https://cirosantilli.com/linux-kernel-module-cheat#cpp + +#include + +template +struct MyClass { + T myVal; + MyClass(T myVal) : myVal(myVal) {} + T myFunc() { + return myVal + 1; + } +}; + +int main() { + assert(MyClass(1).myFunc() == 2); + assert(MyClass(1.5).myFunc() == 2.5); +}