Because I always forget this stuff, I'm putting it here.
Output of program:
cool
cool
cool
not cool
Because slicing ain't cool.
#include <iostream> | |
using namespace std; | |
class A { | |
public: | |
A() = default; | |
virtual ~A() = default; | |
virtual void Talk() const { | |
cout << "not cool" << endl; | |
} | |
}; | |
class B : public A { | |
public: | |
B() = default; | |
~B() override = default; | |
void Talk() const override { | |
cout << "cool" << endl; | |
} | |
}; | |
int main() | |
{ | |
B thing; | |
A* ptr = &thing; | |
A& ref = thing; | |
A slice = thing; | |
thing.Talk(); | |
ptr->Talk(); | |
ref.Talk(); | |
slice.Talk(); | |
return 0; | |
} |