OnJava8-Examples/generics/InstantiateGenericType.cpp

18 lines
316 B
C++
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: generics/InstantiateGenericType.cpp
// 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
} ///:~