Skip to content

Instantly share code, notes, and snippets.

@scvalex
Last active December 16, 2015 21:59
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/5504130 to your computer and use it in GitHub Desktop.
Save scvalex/5504130 to your computer and use it in GitHub Desktop.
Fun with multiple inheritance and pointers in C++
#include <cstdio>
#include <inttypes.h>
using namespace std;
class Left {
int32_t a, b;
};
class Right {
int32_t c, d;
};
class Bottom : public Left, public Right {
};
int main(int argc, char *argv[]) {
Bottom *bottom = new Bottom();
Left *left = bottom;
Right *right = bottom;
printf("left -> %p\n", left);
printf("bottom -> %p\n", bottom);
printf("right -> %p\n", right);
if (left == bottom && bottom == right) {
printf("left == bottom == right\n");
} else {
printf("!(left == bottom == right)\n");
}
return 0;
}
% g++ -Wall -o mi mi.cpp && ./mi
left -> 0x24dd010
bottom -> 0x24dd010
right -> 0x24dd018
left == bottom == right
@scvalex
Copy link
Author

scvalex commented Jun 25, 2013

The associated blog post is here.

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