In a C ++ project that is filled with objects (with proper behavior) and a relatively small number of non-object-oriented structures (consisting of only data fields and no methods), I would like to prevent accidental abuse of these structures, in which you can try to create a class inheriting from it.
According to my understanding, since these "POD" structures (plain old data) do not have a virtual destructor, it is impossible to correctly remove the object of the derived class (if it can be created) using the pointer type POD.
This seems to be well suited for the C ++ 11 final keyword, which marks a class or structure as non-inherited.
However, I wonder if the keyword βfinalβ arises so that the structure becomes a non-POD?
I suspect that the standards documentation may solve this problem, but I'm not smart enough to sift through very long documents to figure them out. Any useful pointers would be appreciated.
Note. I'm not interested in just knowing that it passes compilation of some compiler providers. Passing the compilation does not guarantee:
- Will the compiled code execute correctly in all situations (especially when the technique is used in a larger and more complex project),
- Is this supposed to be used by the C ++ standards body in this way.
#include <iostream> using namespace std; struct Pod final { int a; int b; int c; }; #if 0 class FailsBecauseCannotDeriveFromFinalAnything : public Pod { }; #endif class ContainsSomethingFinalAsMember { public: ContainsSomethingFinalAsMember() : pod() {} private: Pod pod; }; int main() { std::cout << std::is_pod < Pod > :: value << std::endl; return 0; }
source share