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

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  [RESUELTO] FUNCIONES GLOBALES CLASS
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [RESUELTO] FUNCIONES GLOBALES CLASS  (Leído 1,850 veces)
Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
[RESUELTO] FUNCIONES GLOBALES CLASS
« en: 20 Diciembre 2013, 15:39 pm »

Hola a todos, me estaba preguntando cómo podría hacer yo una clase que tenga funciones globales, y que a su vez permita templates ej:

MiClase::Mensaje("hola"); //función común

//Template
a y b = int
MiClase::Suma(a, b);

a y b = float
MiClase::Suma(a, b);

No sé si es mejor una clase o un namespace, para mi es todo de lo mismo *-)

Desde yá muchas gracias.


« Última modificación: 20 Diciembre 2013, 19:00 pm por Miseryk » En línea

Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It’s never too late to change our luck
So, don’t let them steal your light
Don’t let them break your stride
There is light on the other side
And you’ll see all the raindrops falling behind
Make it out tonight
it’s a revolution

CL!!!
ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re:
« Respuesta #1 en: 20 Diciembre 2013, 16:19 pm »

Template<class T> MiClase(T a, T b);

Así?

Enviado desde mi ST21i mediante Tapatalk


En línea

Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
Re: [AYUDA] FUNCIONES GLOBALES CLASS
« Respuesta #2 en: 20 Diciembre 2013, 16:55 pm »

En realidad estoy buscando algo extremadamente fácil que podía hacer en VB6 que era como declarar un módulo y llamarlo así:

Module1.Función()

Algo así, que la clase tenga solamente funciones y variables globales para llamar desde cualquier lado, como RandomNumber, Suma, Resta, blabla, pero no puedo hacerlo funcionar con template<typename T> T Suma(T a, T b) por ejemplo, porque cuando trato de compilar me tira errores que ni idea que són...

Si tengo la estrucura en el .h, al escribirlo en el .cpp se llama sólo? cómo funciona?

Ej:
Tengo GlobalMisery.h y GlobalMisery.cpp

Yo estoy en main.cpp

Si hago un include a GlobalMisery.h, se llama a su vez a GlobalMisery.cpp?

Porque en ese cpp tengo el código de la estructura del .h

El tema de punteros está todo muy lindo, pero con este tema de templates es horrible, yo no lo puedo hacer funcionar :(
En línea

Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It’s never too late to change our luck
So, don’t let them steal your light
Don’t let them break your stride
There is light on the other side
And you’ll see all the raindrops falling behind
Make it out tonight
it’s a revolution

CL!!!
xaps

Desconectado Desconectado

Mensajes: 157



Ver Perfil
Re: [AYUDA] FUNCIONES GLOBALES CLASS
« Respuesta #3 en: 20 Diciembre 2013, 17:37 pm »

Al usar templates has de incluir la implementación desde la cabecera de la clase añadiendo #include "tu_cpp.cpp" al final de tu archivo de cabecera.
Otra opción es la de tener la cabecera y la implementación en el mismo fichero, pero no lo recomiendo si el código es muy extenso.

Saludos
En línea

"The programmers of tomorrow are the wizards of the future" - Gave Newel
Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
Re: [RESUELTO] FUNCIONES GLOBALES CLASS
« Respuesta #4 en: 20 Diciembre 2013, 19:00 pm »

Traté de hacerlo todo en el .h, pero es una basura, hay cosas que no se pueden pisar, como las variables static (al menos yo no pude, y lo intenté mucho y busqué mucho en internet, todos siguen la lógica de usar el .cpp....)

PERO PUDE LOGRARLO!

Código
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Filename: GlobalMisery.h
  3. ////////////////////////////////////////////////////////////////////////////////
  4. #ifndef _GLOBALMISERY_H_
  5. #define _GLOBALMISERY_H_
  6.  
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // Class name: GlobalMiseryClass
  9. ////////////////////////////////////////////////////////////////////////////////
  10. class GlobalMiseryClass
  11. {
  12. public:
  13. static int Mier**;
  14.  
  15. GlobalMiseryClass();
  16. ~GlobalMiseryClass();
  17.  
  18. template<typename T> static void deleten(T *&ptr)
  19. {
  20. delete ptr;
  21. ptr = 0;
  22. }
  23.  
  24. template<typename T> static T Suma(T a, T b)
  25. {
  26. return a + b;
  27. }
  28. };
  29.  
  30. #endif
  31.  

Código
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Filename: GlobalMisery.cpp
  3. ////////////////////////////////////////////////////////////////////////////////
  4. #include "GlobalMisery.h"
  5.  
  6. //Necesito esta mier** para que compile...
  7. int GlobalMiseryClass::Mier** = 0;
  8.  
  9. GlobalMiseryClass::GlobalMiseryClass()
  10. {
  11.  
  12. }
  13.  
  14. GlobalMiseryClass::~GlobalMiseryClass()
  15. {
  16.  
  17. }
  18.  

Código
  1.          #include "GlobalMisery.h"
  2.  
  3. ...
  4.  
  5.         int* a = new int;
  6.  
  7. GlobalMiseryClass::deleten(a);
  8.  
  9. GlobalMiseryClass::Mier** = 3;
  10.  
  11.          int b;
  12. int c;
  13. int d;
  14.  
  15. b = 1;
  16. c = 2;
  17.  
  18. d = GlobalMiseryClass::Suma(b, c);
  19.  

Gracias por el aporte (Y).
« Última modificación: 20 Diciembre 2013, 21:51 pm por Miseryk » En línea

Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It’s never too late to change our luck
So, don’t let them steal your light
Don’t let them break your stride
There is light on the other side
And you’ll see all the raindrops falling behind
Make it out tonight
it’s a revolution

CL!!!
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Problema funciones, valores globales y demas
PHP
Azielito 3 2,121 Último mensaje 31 Julio 2007, 18:17 pm
por Sanjuu
Problema con funciones (resuelto)
Programación C/C++
Gotttlieb 2 2,401 Último mensaje 10 Marzo 2011, 15:41 pm
por Gotttlieb
[RESUELTO] Llamar a un tag custom (class,style pues estilo)
Desarrollo Web
Servia 0 1,728 Último mensaje 29 Mayo 2011, 20:45 pm
por Servia
{RESUELTO} ¿Cómo redimensiono un jPanel? {RESUELTO} « 1 2 »
Java
|Miguel| 12 10,569 Último mensaje 13 Febrero 2012, 15:11 pm
por |Miguel|
Ejercicio con funciones(Resuelto)
PHP
Maistere 4 2,090 Último mensaje 23 Octubre 2013, 12:29 pm
por Maistere
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines