[comp.lang.c++] Bit field space crunching between base/derived classes: Why not?

jimad@microsoft.UUCP (Jim ADCOCK) (05/01/90)

In article <133@logo.procase.UUCP> roger@procase.UUCP (Roger H. Scott) writes:
>In article <54228@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
>>Re: allowing compilers to lay out members in any order they choose.
>>
>>The present C++ rule being that compilers are allowed to lay out members
>>in any order that are in separate sections as distinguished by public:
>>private: protected: markers, but within each region declaration
>>order is respected.
>>...
>>Also, these ordering rules make it easy to iterate over a certain set
>>of members in an object -- if I couldn't guarantee an ordering of those
>>members, it would be much harder to iterate over them.
>
>How do you "iterate" over members, given that the pointers-to-members aren't
>ordered (you can == and !=, but you can't <, <=, >, or >= them)?  Even if you
>could do this, what about the following (at least with cfront)?
>
>	class Jimad {
>	    int m1;
>	    int m2;
>	    virtual void f(); // doesn't look like data to me ...
>	    int m3;
>	    int m4;
....

First, let me state that when I do screw up in C++ -- which I do often,
I always try to screw up in much more imaginative ways than you
propose for me.  

Anyway, consider:


class PART
{
	const char* name;
public:
	PART(const char* aname) : name(aname) {}
	void Print() { printf("%s ",name); }
};

class WHOLE
{
	PART head, body, tail;
public:
	WHOLE(char* h, char* b, char* t) : head(h), body(b), tail(t) {}
	void Print() 
	{
		for (PART* p = &head; p<=&tail; ++p)
			p->Print();
	}
};

main()
{
	WHOLE whole("head", "body", "tail");
	whole.Print();
}

// accepted, and runs as expected under 2.0

// Use member pointers where the pointer must be bound to a particular object
// before it can make sense.  Use a regular pointer to refer to one particular
// object in the system at a given time.  IE a member pointer to body refers
// to all the bodies of wholes in the system -- you must specify a particular
// whole to specify which body.  But, that doesn't stop you from using a
// regular PART* to point to one particular whole's body.