I got what POD means, and I know that when I declare a structure in C++ as
struct f {};
there’s a default constructor, a default copy constructor, a default destructor, etc.. (if I got this correctly)
My question is: how can I declare a POD structure with just plain data (like 4 int values) without implicit constructors/destructors/etc.. getting in the way?
Did I miss something?
Every object type has a constructor—else how would you construct it?—and a destructor—else how would it be destroyed? They don’t “get in the way” of anything, because the default implementations are no-ops in the case of POD-only fields, just as if you had said:
However, if you write an empty constructor, the type becomes non-POD. C++11 solves this with defaulting:
The situation is slightly different with copy constructors, where the implicitly generated one is just a convenience that does “the right thing” for POD types:
There is no reason to rewrite this yourself. If you want to make a type noncopyable, you can mark the copy constructor as deleted with
f(const f&) = delete;or declare itprivate.It’s also important to note that member functions are not stored on the object as member variables are. You can think of a
classorstructas simultaneously two things:A description of a data layout
A namespace containing functions and types for manipulating that data
The C++ model of object-oriented programming simply couples these two things in one place.