Skip to content

Instantly share code, notes, and snippets.

@scvalex
Created May 11, 2013 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scvalex/5558574 to your computer and use it in GitHub Desktop.
Save scvalex/5558574 to your computer and use it in GitHub Desktop.
Subclassing in C++: overriden fields are not removed, the syntax for accessing them is just unusual. This is not a surprise, since subclassing strictly extends objects.
#include <cstdio>
using namespace std;
class A {
public:
int fa;
};
class B : public A {
public:
int fa;
int fb;
};
int main(int argc, char *argv[]) {
B b;
b.A::fa = 2;
b.fa = 3;
b.fb = 5;
printf("b.A::fa = %d; b.fa = %d; b.fb = %d\n", b.A::fa, b.fa, b.fb);
return 0;
}
@scvalex
Copy link
Author

scvalex commented May 11, 2013

Build and run with:

% g++ -Wall -o subclass subclass.cpp && ./subclass
b.A::fa = 2; b.fa = 3; b.fb = 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment