C ++ structure inheritance

As far as I know, the main difference (and can be unique) between classes and structures in C ++ is that classes have their own members by default, and structures have their own public values ​​by default.

However, it is possible because I used to be a C developer, I still continue to declare structures to store only β€œpublic” fields, and I almost never declare methods (other than constructors to initialize elements).

I also want to use C ++ inheritance for structures.

My questions:

  • Even if the language allows this, does the structure inherit correctly?
  • Is it possible to prevent a struct from declaring a virtual method that will create a vtable and resize the structure?

thanks

+5
source share
6 answers

classes have their private members by default, and structures have their public defaults.

Structures also inherit the default publication, where classes are closed by default by default.

Even if the language allows it, is it a good practice to inherits structs ? 

Sure. It works exactly as you expected.

Is it possible to prevent a struct from declaring a virtual method that will create a vtable and resize the structure?

Not yet. There is a suggestion for C ++ 20+ ( P0707 ) so that this can be done, but it is still quite young and not implemented far enough for use anywhere. In particular, search for β€œ3.6 plain_struct” to see how they apply simple structures.

In general, I would recommend using a structure when you use it as a function of type "struct" - data storage without invariants. If you have invariants, you should store them using encapsulation and data hiding, so it should be a class.

+7
source

Just want to solve this issue:

Even if the language allows this, does the structure inherit correctly?

You must get rid of the connotation that "struct" indicates POD. Sometimes the most reusable components are those that do not encapsulate anything, despite some behavior.

For example, consider this meta function:

 template<typename T> struct is_foo : std::false_type {}; template<> struct is_foo<Foo> : std::true_type {}; 

All of the above types (and types beyond aliases for true and false) are declared with the struct keyword. This is simply because everything that is open by default redirects the behavior we want, without having to specify it every time.

Another time, when you discover that it inherits from "struct", this is a C library extension. If the library defines a structure called struct Bar that is used to communicate with it, the easiest way is to add functionality to it by inheriting from Bar . Like this:

 class ExtendedBar : Bar { void mem_func() { //Need to call the C library function? No problem c_library_func(this); // ExtendedBar is-a Bar } }; 

The only important difference is the default accessibility levels. And the only thing you need to dedicate (IMO) is that default accessibility is best for your purpose.

+4
source

Even if the language allows this, does the structure inherit correctly?

Yes it is. Just browse through C ++ STL (standard template libraries). You will find the structure in abundance.

Is it possible to prevent a struct from declaring a virtual method that will create a vtable and resize the structure?

No ... for now. Once you declare virtual functions .. vtable will be created for struct

+2
source

As far as I know, the main difference (and can be unique) between classes and structures in C ++ is that classes have their own members by default, and structures have their own public values ​​by default.

The only difference between classes declared with the struct keyword and those declared with the class keyword is indeed the default access specifier (which also applies to databases as well as members).

In fact, the easiest way to understand structures is to understand that they are classes.

Even if the language allows this, does the structure inherit correctly?

Sure. It is enough to inherit a class, and structs are classes.

Is it possible to prevent a struct from declaring a virtual method that will create a vtable and resize the structure?

No, there is no such possibility, as far as I know.

+1
source

Structures versus classes

You are right, the main difference between a structure and a class in C ++ is the default access levels. Without an explicit access modifier, class members are private, and structure members are public. Struct elements can also be made private using an access modifier. Keep in mind that this also applies to inherited classes and structures.

As for the general recommendation: many adhere to the use of structures only for data and classes for everything related to behavior [1]. In other words, structures for POD types (Ordinary old data) [2]. This is a very common practice. This does not mean that you cannot have functionality related to accessing and setting up data members, setting up constructors, destructors, etc. "If more functions are required, the class is more suitable. If in doubt, make it a class." Their guide also recommends structures instead of classes for functors and attributes.

You must bear in mind, in addition to any technical problems or shortcomings, there are other reasons for applying specific practices and standards in a team and on a project basis. As also mentioned in the Google Style Guide, we can add our own semantic meaning to the data structures that we use. As a member of the team, I would like to know if the structures have behavior or not. It would be nice to know, for example, that all structures are just POD types.

The Joint Strike Fighter coding standard defines "The structure should be used to model an object that does not require an invariant." Although "the class should be used to model an object that supports the invariant." And that public and secure data should only be used in structures, not in classes. Their rationale is that the class cannot control access to public members; therefore, all data in the class must be private. The needs of your project should always be considered when deciding on coding standards.

Structure inheritance

When considering inheritance, you should consider what inheritance is, not personal inheritance. You should remember what access levels the new ones obtained from them have, and if it makes sense to inherit your structures. Struct elements can be private; if you inherit this, the derived will not have access to the underlying private members.

 struct base { int public_data; private: int private_data; }; struct derived : base { derived() { public_data = 1; // private_data = 1; // no access, won't compile } }; 

In other words, inheritance can be seen as a logical problem rather than an implementation.

There is nothing technically fundamentally wrong with the inheritance of structures. It can be a benevolent practice, and in some cases it can be useful and make a lot of sense.

Keep in mind that in C ++, structures can be inherited from classes and vice versa.

See this question for more information on vtables: When is a vtable created in C ++?

[1] https://google.imtqy.com/styleguide/cppguide.html#Structs_vs._Classes

[2] http://en.cppreference.com/w/cpp/concept/PODType

+1
source

Given struct A { ... }; , struct B { A a; ... } struct B { A a; ... } much safer than struct B : A { ... } .

I suggest not inheriting better than inheriting with a non-virtual destructor. The only thing you lose is the implicit conversion from B* , B& to A* , A& etc. However, you still have explicit B b; ba B b; ba for these circumstances.

-2
source

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


All Articles