Les références

Sources d'information

Librairie(s) requise(s)

iostream

Comment ça marche

En c on a les pointeurs seulement.

En c++ on a en plus les références qui sont une sorte d'alias

C'est un pointeur qui est constant, toujours de-référencé et jamais null.

A partir du moment où la référence va pointer sur une variable, il est alors impossible de changer ce sur quoi la référence pointe. C'est donc un peu comme un pointeur constant.

#include <iostream>

    int main ()
    {
        int		numberOfBalls = 42;
    
        int*	ballsPtr = &numberOfBalls;
        int&	ballsRef = numberOfBalls;
        //int&	ballsRef; // faux, ne fonctionne pas, la ref pointe sur rien
    
        std::cout << numberOfBalls << " " << *ballsPtr << " " << ballsRef << std::endl;
    
        *ballsPtr = 21;
        std::cout << numberOfBalls << " " << *ballsPtr << " " << ballsRef << std::endl;
        ballsRef = 84;
        std::cout << numberOfBalls << " " << *ballsPtr << " " << ballsRef << std::endl;
    
        return (0);
    }
42 42 42
21 21 21
84 84 84   

Les passages de paramétres par référence

Appliquer la règle suivante : Si à un moment ça ne doit pas exister, utiliser un pointeur. Si ça doit toujours exister et ne jamais changer, utiliser une référence

#include <iostream>
#include <string>

void byPtr(std::string* str)
{
    *str += " and ponies";
}

void byConstPtr(std::string * str)
{
    std::cout << *str << std::endl;
}
void byRef(std::string& str)
{
    str += " and dragonflies";
}
void byConstRef(std::string & str)
{
    std::cout << str << std::endl;
}

int main()
{
    std::string str = "I like butterflies";

    std::cout << str << std::endl;
    byPtr(&str);
    byConstPtr(&str);

    str = "I like others";
    byRef(str);
    byConstRef(str);

    return(0);

}
I like butterflies
I like butterflies and ponies
I like others and dragonflies

Retour par référence

#include <iostream>
#include <string>

class Student
{
    private:
        std::string _login;

    public:
        Student(std::string login) : _login(login)
        {
            std::cout << "Student " << this->_login << " is born" << std::endl;
        }

        std::string&    getLoginRef()
        {
            return this->_login;
        }

        std::string const & getLoginRefConst() const
        {
            return this->_login;
        }

        std::string*    getLoginPtr()
        {
            return &(this->_login);
        }
        
        std::string const * getLoginPtrConst() const
        {
            return &(this->_login);
        }

        ~Student()
        {
            std::cout << "Student " << this->_login << " died" << std::endl;
        }

};

int main ()
{

    // Allocation sur la stack (sur la pile) et desalloue automatiquement
    Student     	bob = Student("bfubar");
    Student const	jim = Student("jfubar");

    std::cout << bob.getLoginRefConst() << " " << jim.getLoginRefConst() << std::endl;
    std::cout << *(bob.getLoginPtrConst()) << " " << *(jim.getLoginPtrConst()) << std::endl;

    bob.getLoginRef() = "bobfubar";
    std::cout << bob.getLoginRefConst() << std::endl;

    *(bob.getLoginPtr()) = "bobyfubar";
    std::cout << bob.getLoginRefConst() << std::endl;

}
bfubar jfubar
bobfubar
bobyfubar
Student jfubar died
Student bobyfubar died

retour


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