Reference (C++) - Polymorphic Behaviour

Polymorphic Behaviour

Continuing the relationship between references and pointers (in C++ context), the former exhibit polymorphic capabilities, as one might expect:

#include using namespace std; class A { public: A {} virtual void print { cout << "This is class A\n"; } }; class B: public A { public: B {} virtual void print { cout << "This is class B\n"; } }; int main { A a; A& refToa = a; B b; A& refTob = b; refToa.print; refTob.print; return 0; }

The source above is valid C++ and generates the following output:
This is class A

This is class B

Read more about this topic:  Reference (C++)

Famous quotes containing the word behaviour:

    When we read of human beings behaving in certain ways, with the approval of the author, who gives his benediction to this behaviour by his attitude towards the result of the behaviour arranged by himself, we can be influenced towards behaving in the same way.
    —T.S. (Thomas Stearns)