On 25 December 2010 21:04, ronny meeus wrote: > > I agree, but the original idea was that these code was used for C only > (not C++). > Is the code also not correct for C? C structs cannot have virtual functions, so no. When you introduce virtual functions your structs are not POD types and are not layout-compatible with a C struct. If you want objects that can be used from both C and C++ then you need to avoid using C++ features which introduce incompatibilities. >> Why not just assign to list->next? >> You could still do that with a macro if you have some (probably >> questionable) reason to avoid templates. >> > > It is a bit more complex than that. The name of the member is not always "next". > We take only the assumption that the next pointer is the first member > of the structure / class and then we cast it in the macro to a void > pointer. > > We cannot use templates since the code is both used for C and C++ code. You could put the next pointer in a separate struct which has no virtual functions, so it has the same layout in C and C++, then use that as a base class for a polymorphic C++ type. The C code manipulating the next pointer would only deal with the portable type. Or you could create an interface for manipulating the linked lists, compile the implementation as C++, and always modify the lists through that interface. There are several portable solutions, but casting polymorphic types to void* and hoping they behave like simple C structs is not one of them.