[ create a new paste ] login | about

Link: http://codepad.org/jtFKdeAe    [ raw code | fork ]

C++, pasted on Dec 11:
#ifndef g_inventory_h_
#define g_inventory_h_
#include <string>
#include <iostream>
using namespace std;

template <class Type>
class inventory
{
public:
       inventory(Type=0);
       ~inventory();
       operator Type();
       void vypis();
       Type& operator()(int,int);
       int x();
       int y();

private:        
	int rozmer_x;
	int rozmer_y;
	Type **pole;
};

template <class Type>
inventory<Type>::inventory(Type inicializace)
{
   rozmer_x = 1;
   rozmer_y = 1;
   pole = new Type* [1];
   pole[0] = new Type[1];
   pole[0][0] = inicializace;
}

template <class Type>
inventory<Type>::~inventory()
{
   delete [] pole;
}

template <class Type>
inventory<Type>::operator Type()
{
   return pole[0][0];
}

template <class Type>
int inventory<Type>::x()
{
   return rozmer_x;
}

template <class Type>
int inventory<Type>::y()
{
   return rozmer_y;
}

template <class Type>
void inventory<Type>::vypis()
{
   for (int x=0; x<rozmer_x; x++)
   {
       for (int y=0; y<rozmer_y; y++)
           std::cout << pole[x][y] << "\t";
       std::cout << std::endl;
   }
}

template <class Type>
Type& inventory<Type>::operator()(int zapis_a,int zapis_b)
{	
   if(rozmer_x < zapis_a || rozmer_y < zapis_b)
   {
	   
    //vytvor temp
		Type **temp = new Type*[rozmer_x];
		for(int i = 0; i < rozmer_x; ++i)
			temp[i] = new Type[rozmer_y];
	
	//dosad do temp
    for(int i = 0; i < rozmer_x; ++i)
            for(int j = 0; j < rozmer_y; ++j)
                    temp[i][j]=pole[i][j];
		
    //nove pole
    delete [] pole;
    pole = new Type*[(zapis_a>rozmer_x) ? zapis_a : rozmer_x];
    for(int i = 0; i < ((zapis_a>rozmer_x) ? zapis_a : rozmer_x); ++i)
            pole[i] = new Type[(zapis_b>rozmer_y) ? zapis_b : rozmer_y];

	//dosad do pole
    for(int i = 0; i < ((zapis_a>rozmer_x) ? zapis_a : rozmer_x); ++i)
            for(int j = 0; j < ((zapis_b>rozmer_y) ? zapis_b : rozmer_y); ++j)
				if(i<rozmer_x && j<rozmer_y)
					pole[i][j]=temp[i][j];
				else
					pole[i][j]=0;

	//odstran temp
    delete [] temp;

    //nove souradnice
    rozmer_x = ((zapis_a>rozmer_x) ? zapis_a : rozmer_x);
    rozmer_y = ((zapis_b>rozmer_y) ? zapis_b : rozmer_y);
   }
   return pole[zapis_a-1][zapis_b-1];
}
#endif


Create a new paste based on this one


Comments: