Inheritance

I find my naming conventions pretty annoying. I seem to rewrite child specific names too much. In my example below, I have a widget that has -Connection that has -Config. Each of these objects has specialized classes for the Foo and Bar types.

So my FooWidget has a Foo-Connection that has a Foo-Config. The same goes for the bar. In C ++, I got nine different header files.

  • widget.h
  • connection.h
  • config.h
  • foo_widget.h
  • foo_connection.h
  • foo_config.h
  • bar_widget.h
  • bar_connection.h
  • bar_config.h

I canโ€™t help, but I look at it, I feel that it is wrong. My instincts tell me that something needs to be changed. It looks like a design template that you can use. But I canโ€™t find a better solution.

So, I think my question is: Is there a design template that will improve this design?

enter image description here

- It is difficult for me to formulate my question correctly. Please excuse me, I will clarify when the question becomes more clear.

+5
source share
2 answers

First of all, there is nothing wrong to do something this way. Many people like to put one class on the title, in general. If you donโ€™t like looking at a lot of headings, you can organize them by catalog or filter of the project tree, etc.

This is all a matter of style. As long as the functionality is not touched, the best option is the function of this ridiculous feeling that you have, and the same for people supporting your code in the future. Maybe this answer may make this ridiculous feeling go to you, or maybe not.

Secondly, it is very dependent on the use of each class. If the Foo and Bar classes are small, they can be placed in the header containing the encapsulating class. Perhaps add a forward declaration for the parent class and end the declaration at the bottom of the heading. Bonus points, if they are all connected (for example, FooConnection is a subclass of Foo ), as this reduces the declaration space.

If they are relatively small classes, there can be no harm in declaring the Foo and Bar classes in the definition of their base class. Then there is no FooConnection - actually a Connection::FooConnection . In this case, Connection not only defines the base for FooConnection , it has its own definition . This means that every time you use FooConnection in your code, you think in the context of Connection . I rarely do this myself, primarily because I donโ€™t like typing Connection:: all the time, but also because there are so few cases where I use only a class in one context.

If the encapsulated classes are protected or private , then they are used only by the parent class itself (and friend classes and, possibly, subclasses that you have). Then, since the restricted context is set, and if class declarations are small (<50 or 100 lines), you can declare FooConnection and BarConnection within Connection and maintain readability.

Bonus Point: If you end up declaring classes inside classes, you can use namespace splitting. I do not know the use of these classes, but I assume that FooConnection not the connection itself, but Foo , which belongs to Collection . So, you can declare a class Foo and Bar for each of Widget , Connection and Config .

0
source

I donโ€™t think that something is wrong with the structure of your classes, but without seeing what these classes do, it will be difficult to give advice on how to improve the design.

I believe that a good style has one header file for each class. This simplifies the search, and you can easily use directories (or filters in your IDE) to organize your files. For example, it might be easier for you to structure your files:

 |--Foo |--foo_widget.h |--foo_connection.h |--foo_config.h |--Bar |--bar_widget.h |--bar_connnection.h |--bar_config.h |--widget.h |--connection.h |--config.h 
0
source

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


All Articles