🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#1295
From: Johan Bengtsson
Date: Fri, 18 Dec 1992 15:41
79 lines
3293 bytes
Jim Adcock (jimad@microsoft.com) wrote:
: In article <5386@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
: |That two non-generic pointers of the same type (possibly converted from
: |pointer-to-derived) to different objects or data members should be
: |guaranteed to compare unequal.

Your example (below) shows that at least we have established what we
are disagreeing on.

[...]
[or else] I strongly disagree,
: because I would counterclaim that pointer equality in the following
: example is a legal and perfectly reasonable, and efficient, implementation.
: On the contrary, requiring unnecessary and unexpected padding within
: structures just to meet your new requirements would be extremely 
: undesirable, in my opinion.

But the cases where "unnecessary" padding would be required would
have very minor impact on the majority of programs, at least if I
may consider the current implementation of virtuals (vtbl).  After all,
inheriting from a truly empty base class (no virtuals) is quite uncommon,
don't you think?

If pointer distinctness (as defined above) allows useful semantic constructs,
such as the very common idiom for operator=:
"if ( &other == this ) return", then a small and rare storage overhead
in examples such as the one below could easily be tolerated (IMHO).

: If you want object addresses to be
: unique, then write your code so that they are unique.

You cannot always guarantee that.  Suppose you create a class template
that uses pointers to an unknown class (T*) for identity tests (the
template could be specified to handle only statically allocated objects,
if you like).  That template cannot know that the type parameter (T)
specifies a class that will have distinct pointers to all instances,
_unless_ pointer distinctness is universally guaranteed.

Imagine the confusion that migh occur if you instantiated for example
a Lookup<B*> template class, using class B from your example (Lookup<>
is of course defined by a class/template library).

There are existing commercial class templates that rely on pointer
distinctness.  Are they invalid?

[ Jims example follows, press 'n' if you don't want to see it ]

: #include	<stdio.h>

: class A {};

: class B : public A
: {
: public:
: 	A a;
: };

: main()
: {
: 	B b;
: 	A* p1; 
: 	A* p2;

: 	p1 = &b;
: 	p2 = &(b.a);

: 	if (p1 == p2)
: 		printf("equal %lX %lX\n", (long)p1, (long)p2);
: 	else
: 		printf("unequal %lX %LX\n", (long)p1, (long)p2);

: 	return 0;
: }

-- 
--------------------------------------------------------------------------
| Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
| Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
--------------------------------------------------------------------------
Technical Details
Message-ID: <5450@holden.lulea.trab.se>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
References: <1992Dec16.202800.3398@microsoft.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
From: jbn@lulea.trab.se (Johan Bengtsson)
Newsgroups: comp.std.c++
Subject: Re: Must derived class reserve space for an empty base class?
Message-ID: <5450@holden.lulea.trab.se>
Date: 18 Dec 92 15:41:08 GMT
References: <1992Dec16.202800.3398@microsoft.com>
Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
Lines: 79
X-Newsreader: TIN [version 1.1 + PL8]