C legacy coding style?

A

Alexei Zakharov

I had a pleasure of dealing with some code recently that looked like this:

<code>
struct Node
{
// almost POD, but having non-virtual methods
void foo();
};

struct MyNode
{
// almost POD also

Node node_; // first field
// more data fields...

void bar();
};

// this is how the above structs are used
void StoreNode( Node * );
Node * RetrieveNode();

int main()
{
// store
MyNode * n = new MyNode();
StoreNode( & n->node_ );

// retrieve later
Node * nn = RetrieveNode();
MyNode * mn = ( MyNode * ) nn;
mn->bar(); // actually works with my C++ compiler
}
</code>

I'm well aware that doing so in C++ is extremely fragile and not even
garanteed to work with another C++ compiler.

But this strikes me as a C-style technique which is equivalent to
inheritance in C++. Although in my C days I've never seen it in use. So my
question is if this is (was) widely used in C and if it's valid technique
complying with C standard. (of course methods should be removed from the
structs to compile this code with a C compiler).
 
A

Andrey Tarasevich

Alexei said:
...
But this strikes me as a C-style technique which is equivalent to
inheritance in C++. Although in my C days I've never seen it in use. So my
question is if this is (was) widely used in C and if it's valid technique
complying with C standard. (of course methods should be removed from the
structs to compile this code with a C compiler).
...

Yes, it is a valid C technique. I've seen it used several times and, in
fact, i'm using it myself in my C code right now :) Note that this
technique is considered to be valuable enough to be supported by C++
language for POD-struct types (see 9.2/17) through 'reinterpret_cast'.

AFAIR, in the early years of C++ language some efforts were made to
support this technique even for certain non-POD types. I've seen some
old C++ implementations which placed virtual method table (VMT) pointers
after all data fields of the root class of the hierarchy, thus trying to
extend the functionality now described in 9.2/17 to classes with virtual
methods. After polymorphic classes were excluded, VMT pointer in most
common implementation moved to its more natural place at the very
beginning of the object.
 
N

Nick Hounsome

Alexei Zakharov said:
I had a pleasure of dealing with some code recently that looked like this:

<code>
struct Node
{
// almost POD, but having non-virtual methods
void foo();
};

snip

But this strikes me as a C-style technique which is equivalent to
inheritance in C++. Although in my C days I've never seen it in use. So my
question is if this is (was) widely used in C and if it's valid technique
complying with C standard. (of course methods should be removed from the
structs to compile this code with a C compiler).

If it is required to work with both C and C++ then I suggest that you
conditionally (__cplusplus) compile
out the methods - this will have no effect on the object layout because they
are not virtual
You then have extern "C" global functions implemented in C++ that just call
the member functions.
This way it can be used from C and still have the C++ convenience.
 
P

Phlip

If it is required to work with both C and C++ then I suggest that you
conditionally (__cplusplus) compile
out the methods - this will have no effect on the object layout because they
are not virtual
You then have extern "C" global functions implemented in C++ that just call
the member functions.
This way it can be used from C and still have the C++ convenience.

If that C++ convenience is only saying "aNode.foo()" instead of
"foo(aNode)", it's syntactic sugar. Write clean healthy tested C code, and
cross-compile it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top