Topic: Data member ordering and inheritance
Author: schmidt@liege.ics.uci.edu (Douglas C. Schmidt)
Date: 10 Nov 1993 11:04:11 -0800 Raw View
Hi,
I'm curious whether the following example is/will be
"guaranteed" to work in ANSI C++:
class Message
{
public:
long type;
};
template <class T>
class Typed_Message : public Message
{
public:
T data;
};
int foo (void)
{
Typed_Message<char[100]> m;
write (1, (char *) &m, sizeof m);
}
On implementations of C++ that I have available to me, this particular
code generates a Typed_Message class that places "type" and "data"
adjacent to each other (in that order). Therefore, the "write" of
object "m" will print out exactly 104 bytes (assuming long's are 4
bytes).
Now, my question is whether or not I can reliably count
on this behavior? For sake of argument, let's assume that no other
data fields exist in either Message or Typed_Message and no virtual
function calls are used.
Thanks for any hints!
Doug
--
Douglas C. Schmidt
Department of Information and Computer Science
University of California, Irvine
Irvine, CA 92717. Work #: (714) 856-4105; FAX #: (714) 856-4056
Author: pkt@lpi.liant.com (Scott Turner)
Date: Thu, 11 Nov 1993 15:47:19 GMT Raw View
In article <2brdvb$q17@liege.ics.uci.edu>, schmidt@liege.ics.uci.edu (Douglas C. Schmidt) writes:
>
> class Message
> {
> public:
> long type;
> };
>
> template <class T>
> class Typed_Message : public Message
> {
> public:
> T data;
> };
> On implementations of C++ that I have available to me, this particular
> code generates a Typed_Message class that places "type" and "data"
> adjacent to each other (in that order).
It looks like the C++ standard will *not* guarantee this kind of layout.
The standards committee has added some wording to guarantee the layout
of C-like classes (technically deemed POD-structs). But your class
doesn't qualify because it has a base class.
--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA (508) 872-8700
UUCP: uunet!lpi!pkt Internet: pkt@lpi.liant.com
Author: kanze@us-es.sel.de (James Kanze)
Date: 11 Nov 93 18:01:20 Raw View
In article <2brdvb$q17@liege.ics.uci.edu> schmidt@liege.ics.uci.edu
(Douglas C. Schmidt) writes:
|> I'm curious whether the following example is/will be
|> "guaranteed" to work in ANSI C++:
|> class Message
|> {
|> public:
|> long type;
|> };
|> template <class T>
|> class Typed_Message : public Message
|> {
|> public:
|> T data;
|> };
|> int foo (void)
|> {
|> Typed_Message<char[100]> m;
|> write (1, (char *) &m, sizeof m);
|> }
|> On implementations of C++ that I have available to me, this particular
|> code generates a Typed_Message class that places "type" and "data"
|> adjacent to each other (in that order). Therefore, the "write" of
|> object "m" will print out exactly 104 bytes (assuming long's are 4
|> bytes).
|> Now, my question is whether or not I can reliably count
|> on this behavior? For sake of argument, let's assume that no other
|> data fields exist in either Message or Typed_Message and no virtual
|> function calls are used.
Since this is comp.STD.c++: no you cannot count on this being the
case.
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 15 Nov 1993 08:21:50 GMT Raw View
In article <2brdvb$q17@liege.ics.uci.edu> schmidt@ics.uci.edu (Douglas C. Schmidt) writes:
>Hi,
>
> I'm curious whether the following example is/will be
>"guaranteed" to work in ANSI C++:
>
> class Message
> {
> public:
> long type;
> };
>
> template <class T>
> class Typed_Message : public Message
> {
> public:
> T data;
> };
>
> int foo (void)
> {
> Typed_Message<char[100]> m;
>
> write (1, (char *) &m, sizeof m);
> }
>
>On implementations of C++ that I have available to me, this particular
>code generates a Typed_Message class that places "type" and "data"
>adjacent to each other (in that order). Therefore, the "write" of
>object "m" will print out exactly 104 bytes (assuming long's are 4
>bytes).
>
> Now, my question is whether or not I can reliably count
>on this behavior?
The short answer is "no" Doug. Layout of struct/union/class types is
implementation dependent, and is beyond the scope of the work now being
undertaken in the X3J16 standardization committee.
Note however that independent of the work of X3J16, commercial interests
*are* working towards somewhat less formal "industry" standards with respect
to such matters.
I expect that it will not be long before we will see people announcing C++
"ABIs" for various platforms.
--
-- Ronald F. Guilmette, Sunnyvale, California -------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------