Creating C ++ classes inherits from C Structs, recommended?

I recently did some Windows Api coding (still doing it). And I was trying to find a better way to wrap WNDCLASSEX in a C ++ class when I had this crazy idea, is WNDCLASSEX the right structure? (although it is written in c), and in C ++ structures are treated as classes, so why don’t I declare my WinCLass as a derivative of WNDCLASSEX, so I tried:

class WinClass : protected WNDCLASSEX 

And it worked! Then I tried to use it with SDL structures, but they worked too. But some structures (especially SDL) either do not compile or cause unexplained runtime errors when I extract classes from them. So my question is: Is this type of C structure recommended? Is it really used by professionals, or is it just a lame hack? Should I use for my packers or applications, or will it just lead to inexplicable errors?

+6
source share
3 answers

The only difference between a class and a structure in C ++ is that of access specifiers .

For classes, the default access specifier is private.
For structures, the default access specifier is public.

This means that if you have a class derived from another class / structure, it will be private inheritance by default & If you have a structure belonging to another class / structure, by default it will be public inheritance.

One problem is that since the default access specifier for a structure is publicly available, they can expose members that may not need to be exposed to class inference. In this sense, all encapsulation has a whole new dimension. Of course, you can change the access specifiers inside the structure that you are going to use as a base, then you can avoid the problem, but it may not be possible, since this can affect how the structure is used from other parts of the program.

EDIT:
Thanks to the comments, I have a fair idea what mistakes you have in mind, errors are not specifically related to the fact that you are leaving the structure, but because you misunderstand the rules of the Inheritance and Access specifications.

Here is an example demo program:

 #include<iostream> using namespace std; struct FirstStruct { private: int a; public: int b; FirstStruct():a(10),b(20),c(30) { } protected: int c; }; class MyClass:protected FirstStruct { public: int i; MyClass():i(40),j(50),k(60) { } void doSomething() { a = 100; //private in Base is not accessible in Derived b = 100; //Accessible c = 100; //Accessible i = 100; //Accessible b = 100; //Accessible c = 100; //Accessible } private: int j; protected: int k; }; int main() { MyClass obj; obj.i = 100; //Accessible obj.j = 100; //Error Cannot be Accessed, j is private member obj.k = 100; //Error Cannot be Accessed, k is protected member obj.a = 100; //Error Cannot be Accessed, a is private member in Base Class obj.b = 100; //Error Cannot be Accessed; //b is protected member of MyClass, because of protected Inheritance obj.c = 100; //Error Cannot be Accessed; //same reason as b obj.doSomething(); return 0; } 

You can check the launch of the same here in Ideone.
You will notice that even if you change the base type from struct FirstStruct to class FirstStruct here , you get the same errors (nothing more).

Invite you to see this answer here to understand the rules for inheritance and access specifications.

+1
source

Until you start adding virtual members, everything should be fine. If you add virtual members, the virtual table pointer may cross out your layout memory layout.

This includes virtual destructors!

+2
source

I do not have much experience with the Win API, but I know that in C ++, the only difference between classes and structures is the default visibility. For classes this is private, but for structures it is publicly available. In other words, the class must be a subclass of the structure.

Keep in mind that since this is a good estimate for declaring a virtual destructor for any base class, the structure you are subclassing on will not have it.

0
source

Source: https://habr.com/ru/post/892922/


All Articles