Course list http://www.c-jump.com/bcc/

C++ reference


  1. C++ references
  2. Reference examples
  3. References themselves can't be changed
  4. Reference to a member

1. C++ references



2. Reference examples



3. References themselves can't be changed



4. 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.