🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1170

Re: Must derived class reserve space for an empty base class?

#1170
From: Bjarne Stroustrup
Date: Sun, 27 Dec 1992 14:39
44 lines
1934 bytes

jimad@microsoft.com (Jim Adcock @ Microsoft Corporation) writes

 > Below find an example that frequently catches programmers off guard.
 > Again, C++ pointers DO NOT model any notion of object identity.  If you
 > want your objects to maintain a notion of object identity [A good
 > idea, IMHO] then you must explicitly GIVE your objects a notion of object
 > identity.  C++ comes with no such notion built-in.
 > 
 > #include <iostream.h>
 > 
 > class A { char bsPadding; };
 > class B : public A {};
 > class C : public A {};
 > class D : public B, public C {};
 > 
 > main()
 > {
 > 	D d;
 > 
 > 	B* pb = &d;
 > 	C* pc = &d;
 > 	A* pa1 = pb; 
 > 	A* pa2 = pc; 
 > 
 > 	if (pa1 == pa2)
 > 		cout << "pointers match\n";
 > 	else 
 > 		cout << "pointers don't match -- even though of the same"
 > 			" type and 'to the same object'!\n";
 > 
 > 	return 0;
 > };

Huh? pa1 and pa2 does not point to the same object of type A. They point to
different sub-objects the D each of type A. 

I conjecture that people who think that pa1 and pa2 should point to the
same object aren't confused about `object identity' but about the distinction
between virtual and ordinary base classes (and the reason for that is often
that they haven't read a good C++ textbook, but thought they could deduce
the meaning of the example from first principles or experience with some
other language).
Technical Details
Message-ID: <24490@alice.att.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!skule.ecf!torn!spool.mu.edu!uwm.edu!linac!att!att!allegra!alice!bs
References: <1992Dec16.202800.3398@microsoft.com> <5450@holden.lulea.trab.se> <1992Dec24.202339.26224@microsoft.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!skule.ecf!torn!spool.mu.edu!uwm.edu!linac!att!att!allegra!alice!bs
From: bs@alice.att.com (Bjarne Stroustrup)
Newsgroups: comp.std.c++
Subject: Re: Must derived class reserve space for an empty base class?
Message-ID: <24490@alice.att.com>
Date: 27 Dec 92 14:39:07 GMT
Article-I.D.: alice.24490
References: <1992Dec16.202800.3398@microsoft.com> <5450@holden.lulea.trab.se> <1992Dec24.202339.26224@microsoft.com>
Organization: AT&T Bell Laboratories, Murray Hill NJ
Lines: 44