:::: www.foddex.net ::::
      Home      |      Blog      Tutorials      Reviews      |      Resume      Portfolio      |      Photos      Stuff      |      Unreal      Flame      |      Links
Foddex' head
Contact me

Stuff
NL Weather
NL Precipitation
Temperature
xkcd
ICHC
Slashdot
Kitten
wliia
TBBT - Zoom Zoom!!

BLOG: Multiple inheritance, you gotta love it

posted Sat 04-02-2006 20:04:42, in the c++ (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 {
public:
	virtual ~A() {}
	int a1,a2,a3,a4;
};
class B {
public:
	virtual ~B() {}
	int b1,b2,b3,b4;
};
class M : public A, public B{
public:
	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

[ Back to blog listing ]

3 comment(s)

Burn said at 18-02-2006 20:56

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!

Sickmeister said at 02-05-2006 16:25

I believe the issue here is you talking out of your ass. What have i won?

Kindly,

Sickmeister

Foddex said at 13-05-2006 13:27

Bleh... non-coders :-P

Leave a comment

Name (required)
URL
Copy this code0-458-453-170

Pressure building on my soul
I ask God to take control
Guide me through this fucked up world

-

I know someday you'll have a beautiful life, I know you'll be a star
In somebody else's sky, but why, why, why
Can't it be, can't it be mine