mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 10:15:57 +01:00
c++ template class example
This commit is contained in:
@@ -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
|
||||
|
||||
17
userland/cpp/template.cpp
Normal file
17
userland/cpp/template.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user