What is a C data structure?

I was told to define destructors, copy constructors, assignment operators in all classes that have pointers except where the code may be used by only c code or by c data structures. What is considered C data structures with pointers that would not allow me to use the default constructor?
C does not support classes :). It supports structures of plain-old-data only.

Also, "pointers" is too narrow: all classes that manage a resource should implement the rule of three, whether it's a file, a texture object, plain old dynamic memory, a handle defined by an API somewhere, a network connection or anything else.

Last edited on
So some structs I do not need to implement the rule of three? Say I have c code working with c++ code. Should I just make the not implemented copy ctor and assignment operator private and see if if it compiles?
If you need C code to directly manipulate objects defined in C++ code, those objects must be POD and can't have member functions regardless of whether or not they manage a resource.

If those objects have member functions or are otherwise not POD, the best you can do in C is manipulate pointers to those objects, because C can't handle them.

If you're in this situation, you should consider providing an interface written in C++ with C linkage and use that interface from your C code only. You could need to do this when you're using a C++ library from C code, or otherwise writing one program with both C and C++.

Consider what happens if you have some sort of dumb structure (in C++) that is POD:
 
extern "C" { struct Whatever { whatever_resource handle; } foo; } 

It's an excellent idea to follow the rule of three for this object, since it manages a whatever_resource. Unless you need to use it from C code, where that's nonsense, because C doesn't recognize anything that is not POD.
1
2
/* don't expect this to invoke a constructor. */
struct Whatever *foo = malloc(sizeof (*foo) * 4); 


Last edited on
Topic archived. No new replies allowed.