Use two generic data types in a class definition : generic class « template « C++ Tutorial






#include <iostream>
using namespace std;

template <class Type1, class Type2> class MyClass
{
  Type1 i;
  Type2 j;
public:
  MyClass(Type1 a, Type2 b) {
    i = a;
    j = b;
  }
  void show() {
    cout << i << ' ' << j << '\n';
  }
};

int main()
{
  MyClass<int, double> ob1(10, 0.23);
  MyClass<char, char*> ob2('X', "AAAAAAA");

  ob1.show();
  ob2.show();

  return 0;
}
10 0.23
X AAAAAAA








13.12.generic class
13.12.1.A generic class
13.12.2.Operator overload for generic parameters
13.12.3.Class specialization
13.12.4.array template
13.12.5.Use two generic data types in a class definition