Array of Operator using template

// Arrayoper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

//#define SIZE 20
template <class SType=int , int Size = 500> //  { int Size }-> Non-Type parameter Act As Constant
class arayop
{
SType arr[Size];
public:
arayop()
{
for(int i=0 ; i<Size;i++) arr[i] = i;
}
SType &operator[](int i)
{
//Size = 20; // cant change bcoz Size act as a Constant
if( i < 0 || i > Size )
{
exit(0);
}
return arr[i];
}
};
template<> class arayop<char*>
{
char *str;
public:
arayop()
{
str = new char[500];
str = "Explict template Class specialization";
}
};

int _tmain(int argc, _TCHAR* argv[])
{
arayop<int , 10> obj;
arayop<double> obj2;
arayop<> obj3;
//arayop<20> obj4; // Not Allowed Bcoz parameters taking left to Right
arayop<char*> obj4;
for(double i = 0; i< 10 ; i++)
{
obj[i] = i+10;
}
obj[12] = 100;
return 0;
}

Comments

Popular posts from this blog

Smart Pointers in C++ and How to Use Them

Operator Overloading in C++

How would you read in a string of unknown length without risking buffer overflow