Create a gist now

Instantly share code, notes, and snippets.

@scvalex /mi.cpp
Last active Dec 16, 2015

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
Owner
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