Skip to content

Instantly share code, notes, and snippets.

@browny
Created July 29, 2011 06:09
Show Gist options
  • Save browny/1113253 to your computer and use it in GitHub Desktop.
Save browny/1113253 to your computer and use it in GitHub Desktop.
[C++] Make protected/private inherited members public
#include <iostream>
using namespace std;
class Base {
private:
int a;
int privMethod() { return a; }
public:
Base() { a = 0; b = 0;}
int b;
int pubMethod() { return a; }
};
class Derived1 : protected Base {
public:
Derived1() : Base() {}
};
class Derived2 : protected Base {
public:
Derived2() : Base() {}
//Base::a // Error
//Base::privMethod; // Error
Base::b;
Base::pubMethod;
};
class Test {
public:
Derived1 derived1;
Derived2 derived2;
void test() {
//int b1 = derived1.b; // Error
//int b2 = derived1.pubMethod(); // Error
cout << derived2.b << endl;
cout << derived2.pubMethod() << endl;
}
};
int main() {
Test test;
test.test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment