Created
April 1, 2013 19:59
-
-
Save ddrone/5287301 to your computer and use it in GitHub Desktop.
Operator overriding in 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> | |
| #include <string> | |
| using namespace std; | |
| class A | |
| { | |
| public: | |
| virtual void operator-(const A& other) const; | |
| }; | |
| void A::operator-(const A& other) const { | |
| cout << "Hello from A::operator-" << endl; | |
| return; | |
| } | |
| class B : public A | |
| { | |
| public: | |
| virtual void operator-(const B& other) const; | |
| }; | |
| void B::operator-(const B& other) const { | |
| cout << "Hello from B::operator-" << endl; | |
| return; | |
| } | |
| int main() | |
| { | |
| A * a1 = new B(); | |
| B * b = new B(); | |
| (* a1) - (* a1); | |
| (* b) - (* b); | |
| delete a1; | |
| delete b; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment