473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

C legacy coding style?

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).
Jul 22 '05 #1
3 1151
Alexei Zakharov wrote:
...
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2

"Alexei Zakharov" <res0m4rf__@__verizon.net> wrote in message
news:4D*****************@nwrddc02.gnilink.net...
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.
Jul 22 '05 #3
Nick Hounsome wrote:
<code>
struct Node
{
// almost POD, but having non-virtual methods
void foo();
};
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.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JellBell | last post by:
I dont know what is a legacy system..please help me out Posted Via Usenet.com Premium Usenet Newsgroup Services ---------------------------------------------------------- ** SPEED ** RETENTION...
9
by: Suzie | last post by:
Hi, I've inherited some code from a developer who has moved onto other things. The code makes up a rather simple application: a text editor. The problem is, I'm still a novice programmer and...
4
by: Mark | last post by:
In the Visual C++ 7.1 compiler is there a legacy code mode? This is for non-GUI code. Its easy enough to bring the project in but of course I get a lot of clashes with ISO std C++ conventions. ...
18
by: craig | last post by:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style? style 1: method { }
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
3
by: Geoff | last post by:
I need to keep a few VB6 tcpClients active, but have them talk to a dotNet tcpServer/tcpListener. The "Server"/Listener is running as a Plugin, and needs to respond to "outside requests". The...
4
by: Mike Labosh | last post by:
I realize that you people have not seen much of me other than some framework responses I have posted. I am primarily a VB guy (yes, you can laugh) But I have lurked here for several years,...
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
7
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.