c++ template class example

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-09-07 00:00:00 +00:00
parent 906f7ac625
commit 4806f0ecfb
2 changed files with 19 additions and 0 deletions

17
userland/cpp/template.cpp Normal file
View File

@@ -0,0 +1,17 @@
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#include <cassert>
template <class T>
struct MyClass {
T myVal;
MyClass(T myVal) : myVal(myVal) {}
T myFunc() {
return myVal + 1;
}
};
int main() {
assert(MyClass<int>(1).myFunc() == 2);
assert(MyClass<float>(1.5).myFunc() == 2.5);
}