<<< References themselves can't be changed     Index     Copy constructors >>>

19. Reference to a member


  • 
    // point.h
    class Point
    {
    public:
        int& refx();
    private:
        int m_x;
        int m_y;
    };
    
    // point.cpp
    #include "point.h"
    int& Point::refx()
    {
        return m_x;
    }
    
    // main.cpp
    #include "point.h"
    int main()
    {
        Point pt;
        pt.refx() = 14;
        return 0;
    }
    
    
  • Returning a reference to a class member creates a "trapdoor" or "backdoor" that allows the data to be modified outside of the class regardless whether it is public or private.

  • Trapdoor is a secret way of gaining access to a program, online service, or other resource.


<<< References themselves can't be changed     Index     Copy constructors >>>