Separating a Template from Function Code - C++ template

C++ examples for template:template function

Description

Separating a Template from Function Code

Demo Code

#include <iostream>

using namespace std;

template <typename T>
class MyClass/*from  w w w.jav a2  s .c o m*/
{
protected:
    T x;

public:
    T& getx();
    void setx(T);
};

template <typename T>
T &MyClass<T>::getx()
{
    return x;
}

template <typename T>
void MyClass<T>::setx(T newx)
{
    x = newx;
}

int main()
{
    MyClass<int> separate;
    separate.setx(10);
    cout << separate.getx() << endl;
    return 0;
}

Result


Related Tutorials