Skip to content

Instantly share code, notes, and snippets.

@bravikov
Last active February 18, 2019 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bravikov/1986169505696b766206e2b2089901b9 to your computer and use it in GitHub Desktop.
Save bravikov/1986169505696b766206e2b2089901b9 to your computer and use it in GitHub Desktop.
A reference to an object of a child class
// A reference to an object of a child class
#include <iostream>
class A
{
public:
virtual ~A() {}
virtual void print() { std::cout << "A" << std::endl; }
};
class B: public A
{
public:
void print() override { std::cout << "B" << std::endl; }
};
int main()
{
A a;
B b;
A& a2 = b;
A& a3 = a;
a.print();
b.print();
a2.print();
a3.print();
a3 = b;
a3.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment