Skip to content

Instantly share code, notes, and snippets.

@NigoroJr
Created August 7, 2015 06:39
Show Gist options
  • Select an option

  • Save NigoroJr/2048880b8f0ff0488a08 to your computer and use it in GitHub Desktop.

Select an option

Save NigoroJr/2048880b8f0ff0488a08 to your computer and use it in GitHub Desktop.
#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