How to use Private Inheritance aka C ++ in C # and why it is not present in C #

I know that private inheritance is supported in C ++, and in C # only inheritance is supported. I also stumbled upon an article that said private inheritance usually defines HAS-A relationships and the type of aggregation relationship between classes.

EDIT: C ++ code for private inheritance: The relationship "Car with engine" can also be expressed using private inheritance:

class Engine { public: Engine(int numCylinders); void start(); // Starts this Engine }; class Car : private Engine { // Car has-a Engine public: Car() : Engine(8) { } // Initializes this Car with 8 cylinders using Engine::start; // Start this Car by starting its Engine }; 

Now, is there a way to create a HAS-A relationship between C # classes, which is one of the things I would like to know - HOW?

Another curious question: why does C # not support private (as well as secure) inheritance? - Does multiple inheritance of the implementation support a valid reason or any other?

Is private (and secure) inheritance planned for future versions of C #?

Will support for private (and secure) inheritance in C # make it a better and widely used language?

+4
source share
2 answers

Now, is there a way to create a HAS-A relationship between C # classes, which is one of the things I would like to know - HOW?

Make one class with a field of another class:

 class Car { Engine engine; } 

The car has an engine.

Another curious question: why does C # not support private (as well as secure) inheritance?

You have a box in your basement. You have never invested anything in this. Someone asks you: "Why is this box empty?" What answer can you give, except that the box is empty, because no one puts anything into it?

C # does not support private or secure inheritance, because no one has ever implemented this feature and sent it to clients. Functions are not performed by default, for free. It's not like we started C # with private and secure inheritance, and then pulled them out for some good reason. These features have never been in the first place, and, unsurprisingly, they are still missing. Features do not grow.

Does multiple inheritance of the implementation support a valid reason, or any other?

I do not understand the question.

Is private (and secure) inheritance planned for future versions of C #?

No.

Will support for private (and secure) inheritance in C # make it a better language than it is now?

I do not think so.

One of the many problems with inheritance that we usually see in OOP is that it fully combines the “peculiar” semantic connection with the “repeated implementation details” of the mechanism relationship. Private inheritance partially eliminates this problem.

There are a small number of situations in which I really enjoyed having private inheritance in C #; a recent example was that I had a data structure that could be built at all, but which I did not want to show to users:

 internal class FancyDataStructure<T> {...} 

but it is only possible to serialize when T was int (for reasons not related to the discussion). I would like to say:

 public class SerializableThingy : private FancyDataStructure<int> 

Instead, I simply made a FancyDataStructure<T> nested type SerializableThingy and used composition rather than inheritance. There was a small amount of plumbing code for the record, but it worked out just fine.

I don't think adding a feature pays for itself; this adds to the complexity of the language in exchange for a very small advantage of avoiding some trivial water taxes.

Will support for private (and secure) inheritance in C # make it a more widely used language than it is now?

How could I or did anyone else possibly know the answer to this question? StackOverflow is a poor place to ask questions that require counterfactual forecasting of the future. My suggestions:

  • Deploy the C # version with the required function. See if it will become popular. Then you will find out.
  • Find a person with psychic abilities who can predict what the future will be like if everything is different now. Since counterfactual predictions automatically follow the rules of deductive logic, predictions will be accurate.
+13
source

You should create a Has-a relationship in the usual way: let one class contain an instance of another class.

In C ++, private inheritance is sometimes used to represent interdependent relationships, usually for ease of accessing parent methods / interfaces. This is probably a pretty rare use, that I won't be too worried that C # doesn't offer it, especially if the composition offers pretty similar features.

+1
source

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


All Articles