Created
August 7, 2015 06:39
-
-
Save NigoroJr/2048880b8f0ff0488a08 to your computer and use it in GitHub Desktop.
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> | |
| class Foo { | |
| public: | |
| Foo() {} | |
| Foo(const int x) : x(x) {} | |
| int other_x(const Foo& other) { | |
| // Accessing private member variable | |
| return other.x; | |
| } | |
| private: | |
| int x; | |
| }; | |
| class Derived : public Foo { | |
| public: | |
| Derived() {} | |
| Derived(const int x) : Foo(x) {} | |
| }; | |
| int main() { | |
| Foo f1, f2(4); | |
| Derived d(42); | |
| // Same class | |
| std::cout << f1.other_x(f2) << std::endl; | |
| // Inherited | |
| std::cout << d.other_x(f2) << std::endl; | |
| std::cout << f1.other_x(d) << std::endl; | |
| // Error | |
| //f2.x; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment