Using Different Types for a Template Parameter - C++ template

C++ examples for template:template function

Description

Using Different Types for a Template Parameter

Demo Code

#include <iostream>

using namespace std;

template <typename T>
class MyClass/*from   w  w  w.jav  a2 s.co  m*/
{
public:
    T member;
};

template <int MaxPrice>
class PriceController
{
public:
    int Price;
    void TestPrice()
    {
        if (Price > MaxPrice)
        {
            cout << "Too expensive" << endl;
        }
    }
};

int main()
{
    MyClass<int> myClass;
    myClass.member = 2;
    cout << myClass.member << endl;

    const int c = 30;
    PriceController<c> FredsToaster;
    FredsToaster.Price = 15;
    FredsToaster.TestPrice();

    PriceController<c> FredsDrawingSet;
    FredsDrawingSet.Price = 45;
    FredsDrawingSet.TestPrice();

    const int JulieMaxPrice = 60;
    PriceController<JulieMaxPrice> JuliesCar;
    JuliesCar.Price = 80;
    JuliesCar.TestPrice();

    return 0;
}

Result


Related Tutorials