A coder's home for Marc "Foddex" Oude Kotte, who used to be located in Enschede, The Netherlands, but now in Stockholm, Sweden!

Multiple inheritance, you gotta love it

Originally posted at Sat 04-02-2006 20:04:42, in the c++ category.
If you're a programmer, can you predict what the program below prints? And can you really explain what the issue is here? :)
#include <stdio.h>

class A {
	virtual ~A() {}
	int a1,a2,a3,a4;
class B {
	virtual ~B() {}
	int b1,b2,b3,b4;
class M : public A, public B{
	virtual ~M() {}

int main() {

	A a;
	B b;
	M m;
	m.a1 = 1;
	m.a2 = 2;
	m.a3 = 3;
	m.a4 = 4;
	m.b1 = 11;
	m.b2 = 12;
	m.b3 = 13;
	m.b4 = 14;

	printf(\"A offsets:\\n\");
	printf(\"%d %d %d %d\\n\", &((A*)NULL)->a1, &((A*)NULL)->a2,
                        &((A*)NULL)->a3, &((A*)NULL)->a4);
	printf(\"B offsets:\\n\");
	printf(\"%d %d %d %d\\n\", &((B*)NULL)->b1, &((B*)NULL)->b2,
                        &((B*)NULL)->b3, &((B*)NULL)->b4);
	printf(\"M offsets:\\n\");
	printf(\"%d %d %d %d \", &((M*)NULL)->a1, &((M*)NULL)->a2,
                        &((M*)NULL)->a3, &((M*)NULL)->a4);
	printf(\"%d %d %d %d\\n\", &((M*)NULL)->b1, &((M*)NULL)->b2,
                        &((M*)NULL)->b3, &((M*)NULL)->b4);

	printf(\"values normal cast:\\n\");
	B* bb1 = &m;
	printf(\"%d %d %d %d\\n\", bb1->b1, bb1->b2, bb1->b3, bb1->b4);

	printf(\"values void* cast:\\n\");
	B* bb2 = (B*)(void*)&m;
	printf(\"%d %d %d %d\\n\", bb2->b1, bb2->b2, bb2->b3, bb2->b4);

	return 0;

-- Foddex

3 comment(s)

Click to write your own comment

On Sat 18-02-2006 20:56 Burn wrote: Now I get it!! That's what you asked to the blonde lady 2 weeks ago in Van Speyk to make her go nutz about you!
On Tue 02-05-2006 16:25 Sickmeister wrote: I believe the issue here is you talking out of your ass. What have i won?


On Sat 13-05-2006 13:27 Foddex wrote: Bleh... non-coders :-P
URL: (optional!)
Write your comment:
Answer this question to prove you're human:
What are wooden tables made of?