Last active
December 14, 2015 00:39
-
-
Save maxdeliso/5000660 to your computer and use it in GitHub Desktop.
example of late binding with c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| struct Foo { | |
| virtual int retrieve() = 0; | |
| }; | |
| struct Bar : public Foo { | |
| inline int retrieve() { | |
| return 1; | |
| } | |
| }; | |
| struct Baz : public Foo { | |
| inline int retrieve() { | |
| return 2; | |
| } | |
| }; | |
| int main() { | |
| Bar bar; | |
| Baz baz; | |
| /* outputs "1 2" because of late binding */ | |
| std::cout | |
| << bar.retrieve() << " " | |
| << baz.retrieve() << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment