2015-04-20 15:36:01 -07:00
|
|
|
|
//: generics/InstantiateGenericType.cpp
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
// 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
|
|
|
|
|
} ///:~
|