CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    [RESOLVED] polymorfic sort, overloading operator >

    Hi gurus.

    I need to create a class witch it's operator > are polymorfical.

    This is the code of .h file:

    Code:
    #define CRITERIO_ORDENACION(Fn,a,b,c,d,e) UCHAR (* Fn[5])(const CElementoInstruccion &, const CElementoInstruccion &) = {a,b,c,d,e}
    
    #define STEP CheckStep
    #define INSTRUCCION CheckInstruccion 
    #define ELEMENTO CheckElemento
    #define DISTANCIA CheckDistancia
    #define TRAMO CheckTramo
    
    class CElementoInstruccion {	
    	friend bool operator == (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    	friend bool operator != (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    	friend bool operator > (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    	friend bool operator < (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    	friend bool operator >= (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    	friend bool operator <= (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    private: 
    	void Copy(const CElementoInstruccion &ei);
    	void InicializaOrden(void);
    	UCHAR (* Criterio_Ordenacion[5])(const CElementoInstruccion &, const CElementoInstruccion &);
    public:
    	CElementoInstruccion();
    	~CElementoInstruccion();
    	UINT nStep, nInstruccion, nTramo, nDistancia, nElemento;
    
    	void SetCriterioOrdenacion(UCHAR (* Criterio_Ordenacion[5])(const CElementoInstruccion &, const CElementoInstruccion &));
    
    	CElementoInstruccion & operator =(const CElementoInstruccion &ei);
    	CElementoInstruccion(const CElementoInstruccion &ei);
    
    	bool operator == (const CElementoInstruccion &ei);
    	bool operator != (const CElementoInstruccion &ei);
    	bool operator > (const CElementoInstruccion &ei);
    	bool operator < (const CElementoInstruccion &ei);
    	bool operator >= (const CElementoInstruccion &ei);
    	bool operator <= (const CElementoInstruccion &ei);	
    };
    
    
    UCHAR CheckStep(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    UCHAR CheckInstruccion(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    UCHAR CheckElemento(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    UCHAR CheckDistancia(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    UCHAR CheckTramo(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2);
    And this is the code for .cpp file:

    Code:
    using namespace std;
    
    void CElementoInstruccion::InicializaOrden(void) {		
    	Criterio_Ordenacion[0] = STEP;
    	Criterio_Ordenacion[1] = INSTRUCCION;
    	Criterio_Ordenacion[2] = ELEMENTO;
    	Criterio_Ordenacion[3] = DISTANCIA;
    	Criterio_Ordenacion[4] = TRAMO;
    }
    
    CElementoInstruccion::CElementoInstruccion() {
    	InicializaOrden();
    	nStep = 0;
    	nInstruccion = 0;
    	nTramo = 0;
    	nDistancia = 0;
    	nElemento = 0;
    }
    
    CElementoInstruccion::~CElementoInstruccion() {
    }
    
    void CElementoInstruccion::Copy(const CElementoInstruccion &ei) {
    	for(int i=0;i<5;i++) Criterio_Ordenacion[i] = ei.Criterio_Ordenacion[i];
    	nStep = ei.nStep;
    	nInstruccion = ei.nInstruccion;
    	nTramo = ei.nTramo;
    	nDistancia = ei.nDistancia;
    	nElemento = ei.nElemento;
    }
    
    CElementoInstruccion & CElementoInstruccion::operator =(const CElementoInstruccion &ei) {
    	Copy(ei);
    	return *this;
    }
    
    CElementoInstruccion::CElementoInstruccion(const CElementoInstruccion &ei) {
    	Copy(ei);
    }
    
    bool CElementoInstruccion::operator == (const CElementoInstruccion &ei) {
    	return nStep == ei.nStep && nInstruccion == ei.nInstruccion && nTramo == ei.nTramo && nDistancia == ei.nDistancia && nElemento == ei.nElemento;
    }
    
    bool CElementoInstruccion::operator != (const CElementoInstruccion &ei) {
    	return !operator==(ei);
    }
    
    bool CElementoInstruccion::operator > (const CElementoInstruccion &ei) {	
    	if(Criterio_Ordenacion[0](*this, ei) == 0) return true;
    	if(Criterio_Ordenacion[0](*this, ei) == 1) return false;
    
    	if(Criterio_Ordenacion[1](*this, ei) == 0) return true;
    	if(Criterio_Ordenacion[1](*this, ei) == 1) return false;
    
    	if(Criterio_Ordenacion[2](*this, ei) == 0) return true;
    	if(Criterio_Ordenacion[2](*this, ei) == 1) return false;
    
    	if(Criterio_Ordenacion[3](*this, ei) == 0) return true;
    	if(Criterio_Ordenacion[3](*this, ei) == 1) return false;
    
    	if(Criterio_Ordenacion[4](*this, ei) == 0) return true;
    	if(Criterio_Ordenacion[4](*this, ei) == 1) return false;
    
    	return false;
    }
    
    UCHAR CheckStep(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(ei1.nStep > ei2.nStep) return 0;
    	if(ei1.nStep < ei2.nStep) return 1;
    	return 2;
    }
    
    UCHAR CheckInstruccion(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(ei1.nInstruccion > ei2.nInstruccion) return 0;
    	if(ei1.nInstruccion < ei2.nInstruccion) return 1;
    	return 2;
    }
    
    UCHAR CheckElemento(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(ei1.nElemento > ei2.nElemento) return 0;
    	if(ei1.nElemento < ei2.nElemento) return 1;
    	return 2;
    }
    
    UCHAR CheckDistancia(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(ei1.nDistancia > ei2.nDistancia) return 0;
    	if(ei1.nDistancia < ei2.nDistancia) return 1;
    	return 2;
    }
    
    UCHAR CheckTramo(const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(ei1.nTramo > ei2.nTramo) return true;
    	if(ei1.nTramo < ei2.nTramo) return false;
    	return 2;
    }
    
    
    bool CElementoInstruccion::operator < (const CElementoInstruccion &ei) {
    	if(operator==(ei)) return false;
    	return !operator > (ei);
    }
    
    bool CElementoInstruccion::operator >= (const CElementoInstruccion &ei) {
    	if(operator > (ei)) return true;
    	if(operator == (ei)) return true;
    	return false;
    }
    
    bool CElementoInstruccion::operator <= (const CElementoInstruccion &ei) {
    	if(operator < (ei)) return true;
    	if(operator == (ei)) return true;
    	return false;
    }
    
    void CElementoInstruccion::SetCriterioOrdenacion(UCHAR (* FX_Orden[5])(const CElementoInstruccion &, const CElementoInstruccion &)) {
    	Criterio_Ordenacion[0] = FX_Orden[0];
    	Criterio_Ordenacion[1] = FX_Orden[1];
    	Criterio_Ordenacion[2] = FX_Orden[2];
    	Criterio_Ordenacion[3] = FX_Orden[3];
    	Criterio_Ordenacion[4] = FX_Orden[4];
    }
    
    
    bool operator == (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {	
    	return ei1.nStep == ei2.nStep && ei1.nInstruccion == ei2.nInstruccion && ei1.nTramo == ei2.nTramo && ei1.nDistancia == ei2.nDistancia && ei1.nElemento == ei2.nElemento;
    }
    
    bool operator != (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	return !operator ==(ei1, ei2);
    }
    
    bool operator > (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    
    	if(ei1.Criterio_Ordenacion[0](ei1, ei2) == 0) return true;
    	if(ei1.Criterio_Ordenacion[0](ei1, ei2) == 1) return false;
    
    	if(ei1.Criterio_Ordenacion[1](ei1, ei2) == 0) return true;
    	if(ei1.Criterio_Ordenacion[1](ei1, ei2) == 1) return false;
    
    	if(ei1.Criterio_Ordenacion[2](ei1, ei2) == 0) return true;
    	if(ei1.Criterio_Ordenacion[2](ei1, ei2) == 1) return false;
    
    	if(ei1.Criterio_Ordenacion[3](ei1, ei2) == 0) return true;
    	if(ei1.Criterio_Ordenacion[3](ei1, ei2) == 1) return false;
    
    	if(ei1.Criterio_Ordenacion[4](ei1, ei2) == 0) return true;
    	if(ei1.Criterio_Ordenacion[4](ei1, ei2) == 1) return false;
    	
    	return false;
    }
    
    bool operator < (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(operator==(ei1, ei2)) return false;
    	return !operator > (ei1, ei2);
    }
    
    bool operator >= (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(operator > (ei1, ei2)) return true;
    	if(operator == (ei1, ei2)) return true;
    	return false;
    }
    
    bool operator <= (const CElementoInstruccion &ei1, const CElementoInstruccion &ei2) {
    	if(operator < (ei1, ei2)) return true;
    	if(operator == (ei1, ei2)) return true;
    	return false;
    }
    I want to make this as static member (.h file, remarks in red and bold):

    UCHAR (* Criterio_Ordenacion[5])(const CElementoInstruccion &, const CElementoInstruccion &);

    If this is a static member y don't need to change the order for each object, only in the class. But I don't know how to do this...


    Best regards and thank you in advance.
    Last edited by juanpast; May 9th, 2012 at 10:09 AM. Reason: Title error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured