elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Mi ejemplo de array de punteros con template
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Mi ejemplo de array de punteros con template  (Leído 1,308 veces)
bash

Desconectado Desconectado

Mensajes: 258



Ver Perfil
Mi ejemplo de array de punteros con template
« en: 7 Octubre 2016, 15:58 pm »

buenas a todos
me gustaria compartiles algo que estoy haciendo para aprender mas sobre los punteros y los templates, antes que nada se que podria usar una lista enlazada  y podria evitarme lo de asignarle memoria dinamica con new pero quiero tenerla asi controlada con tamano fijo.
me gustaria sus opiniones.

Código
  1.  
  2.  
  3.  
  4. template<class T>
  5. class FixedArrayTemplate
  6. {
  7. private :
  8.  int max_size;
  9.  int size;
  10.  T *Allocate_array;
  11.  bool Correct;
  12. public:
  13.  FixedArrayTemplate(const unsigned int _size)
  14.      {
  15.          Correct = CreateMemoryArray(_size);
  16.      }
  17.  
  18.  bool Push(T element)
  19.  {
  20.  if(size < max_size)
  21.  {
  22.  Allocate_array[size] = element;
  23.  size++;
  24.  return true;
  25.  }
  26.  else
  27.  return false;
  28.  
  29.  }
  30.     bool isCorrect(){return Correct;}
  31.  
  32.     void Clean()
  33.     {
  34.     if(this->Correct) delete[] this->Allocate_array;
  35.     this->Allocate_array = NULL;
  36.     this->Correct = false;
  37.     size = 0;
  38.     max_size = 0;
  39.     }
  40.  
  41.      bool CreateMemoryArray(int _size)
  42.      {
  43.      if(this->isCorrect())
  44.          this->Clean();
  45.  
  46.  max_size = _size;
  47.  size     = 0;
  48.  Allocate_array = new T[_size];
  49.  if(Allocate_array == 0)
  50.  {
  51. Correct = false;
  52. return Correct;
  53.  }
  54.  else
  55.  {
  56.  Correct = true;
  57.  return Correct;
  58.  }
  59.  
  60.      }
  61.  
  62.  
  63.      T getElement(int e)
  64.      {
  65.      if(( e > 0 && e < size ) && e < this->max_size )
  66.      {
  67.     if(this->Correct)
  68.      return this->Allocate_array[e - 1];
  69.      }
  70.      else
  71.      {
  72.  
  73.     if(this->Correct)
  74.      return this->Allocate_array[size - 1];
  75.      }
  76.      }
  77.  
  78.  
  79.  
  80.     ~FixedArrayTemplate()
  81.     {
  82.            Clean();
  83.     }
  84. };
  85.  
  86.  
  87. int main()
  88. {
  89.  
  90.   char *buffer1 ="buffer 1 ";
  91.   char *buffer2 = "buffer 2";
  92.   char *buffer3 = "buffer 3";
  93.   char *buffer4 = "buffer 4";
  94.   char *buffer5 = "buffer 5";
  95.   FixedArrayTemplate<char *>miArray(20);
  96.   if(miArray.isCorrect())
  97.   {
  98.   miArray.Push(buffer1);
  99.   miArray.Push(buffer2);
  100.   miArray.Push(buffer);
  101.   miArray.Push(buffer3);
  102.   miArray.Push(buffer4);
  103.   miArray.Push(buffer5);
  104.   cout << miArray.getElement(0)<<endl;
  105.   cout << miArray.getElement(1)<<endl;
  106.   cout << miArray.getElement(2)<<endl;
  107.   cout << miArray.getElement(3)<<endl;
  108.   cout << miArray.getElement(4)<<endl;
  109.   cout << miArray.getElement(5)<<endl;
  110.   cout << miArray.getElement(6)<<endl;
  111.   }
  112.  
  113. }
  114.  
  115.  
  116.  


gracias su opinion es muy importante.


En línea

gracias por responder mis dudas
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Array de punteros.
Programación C/C++
soyloqbuskas 3 9,437 Último mensaje 30 Enero 2012, 20:28 pm
por soyloqbuskas
manipulación de punteros (ejemplo con float)
Programación C/C++
david_BS 0 1,767 Último mensaje 3 Abril 2012, 03:26 am
por david_BS
Duda Punteros Dobles/Array de punteros
Programación C/C++
MisterJava 2 4,916 Último mensaje 30 Diciembre 2012, 20:19 pm
por MisterJava
Problema con array de punteros
Programación C/C++
NathanD 4 2,582 Último mensaje 26 Marzo 2013, 21:13 pm
por amchacon
Array de punteros en lenguaje C. « 1 2 »
Programación C/C++
DanielPy 16 6,363 Último mensaje 26 Diciembre 2013, 22:04 pm
por leosansan
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines