21 lines
471 B
C++
21 lines
471 B
C++
// generics/InstantiateGenericType.cpp
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
// C++, not Java!
|
|
|
|
template<class T> class Foo {
|
|
T x; // Create a field of type T
|
|
T* y; // Pointer to T
|
|
public:
|
|
// Initialize the pointer:
|
|
Foo() { y = new T(); }
|
|
};
|
|
|
|
class Bar {};
|
|
|
|
int main() {
|
|
Foo<Bar> fb;
|
|
Foo<int> fi; // ... and it works with primitives
|
|
}
|