Last active
December 16, 2015 21:59
-
-
Save scvalex/5504130 to your computer and use it in GitHub Desktop.
Fun with multiple inheritance and pointers in C++
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 <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; | |
} |
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
% g++ -Wall -o mi mi.cpp && ./mi | |
left -> 0x24dd010 | |
bottom -> 0x24dd010 | |
right -> 0x24dd018 | |
left == bottom == right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The associated blog post is here.