operator overload

Sources d'information

Librairie(s) requise(s)

iostream

Comment ça marche

Pour étendre les opérateurs CPP

1+1 = notation 1 fix avec + entre les deux opérandes

+11 = notation prefixe ⁼ notation fonctionnelle, on a une notation qui se rapproche d'une fonction membre 1.+(1) soit une instance (le premier 1) sur laquelle on fera appel à la fonction membre + à qui on passerait en paramètre 1

11+ = notation postfix pour le calcul sur pile

La surcharge de l'opérateur se fait avec le mot clé "operator" (unaire ou binaire, le ternaire n'est pas surchargeable)

= est un operateur binaire (grace au paramètre caché sous this)

+ est un operateur binaire

3 règles sur les surcharges d'opérateurs :

le fichier main :

    #include "Integer.class.hpp"

    int main ()
    {
    
        Integer x( 30 );
        Integer y( 10 );
        Integer z( 0 );
    
        // sortie << grace a la surcharge
        std::cout << "Value of x : "<< x << std::endl;
        std::cout << "Value of y : "<< y << std::endl;
        y = Integer( 12);
        std::cout << "Value of y : "<< y << std::endl;
    
        std::cout << "Value of z : "<< z << std::endl;
        z = x + y;
        std::cout << "Value of z : "<< z << std::endl;
    
        return (0);
    }

le fichier .hpp :

    #ifndef INTEGER_CLASS_HPP
    # define INTEGER_CLASS_HPP
    
    #include
    
    class Integer {
    
    public:
    
        Integer( int const n );
        ~Integer( void );
    
        int     getValue( void ) const;
    
        // deux surcharges d'opperateur
        // rhs = right on side, left on side est cache = param d'instance
        
        // operateur d'assignation, renvoi de l'instance courante
        Integer &   operator=( Integer const & rhs );
    
        // operateur d'addition, l'instance courante n'est jamais modifiee
        Integer     operator+( Integer const & rhs ) const;
    
    
    
    private:
    
        int     _n;
    
    };
    
    std::ostream & operator<<(std::ostream & o, Integer const & rhs);
    
    #endif

le fichier .cpp :

#include <iostream>
#include "Integer.class.hpp"

    Integer::Integer( int const n ) : _n(n)
    {
        std::cout << "Constructor called with value : "<< this->_n << std::endl;

    }


    Integer::~Integer( void ) 
    {
        std::cout << "Destructor called with value : "<< this->_n << std::endl;
    }

    int     Integer::getValue( void ) const 
    {
        return (this->_n);
    }

    Integer &   Integer::operator=( Integer const & rhs )
    {   
        std::cout << "Assignation operator called from : "<< this->_n;
        std::cout << " to : "<< rhs.getValue() << std::endl;
        
        this->_n = rhs.getValue();

        // retourne l'addresse de this, soit la reference
        return(*this);
    }

    Integer     Integer::operator+( Integer const & rhs ) const
    {
        std::cout << "Addition operator called with : "<< this->_n;
        std::cout << " and : "<< rhs.getValue() << std::endl;

        // creation d'une nouvelle instance de la class Integer
        // pas d'autre solution 
        return Integer(this->_n + rhs.getValue() );
    }

    // surcharge de simple fonction
    // surcharge de ostream <<
    std::ostream & operator<<( std::ostream & o, Integer const & rhs)
    {
        o << rhs.getValue();
        return (o);
    }
    Constructor called with value : 30
    Constructor called with value : 10
    Constructor called with value : 0
    Value of x : 30
    Value of y : 10
    Constructor called with value : 12
    Assignation operator called from : 10 to : 12
    Destructor called with value : 12
    Value of y : 12
    Value of z : 0
    Addition operator called with : 30 and : 12
    Constructor called with value : 42
    Assignation operator called from : 0 to : 42
    Destructor called with value : 42
    Value of z : 42
    Destructor called with value : 42
    Destructor called with value : 12
    Destructor called with value : 30

retour


© 2022, vroch
Revisé le : 13 nov 2022
URL : http://vroch.ch/vr_tech_041.html
Main : http://vroch.ch