P
P
prometian2014-09-24 19:36:43
C++ / C#
prometian, 2014-09-24 19:36:43

How to correctly choose whether to inherit a class or pass a pointer to a class?

There is a class responsible for connecting to the database. Separately, there is a class that generates a request. Does it make sense to use inheritance or is it better to pass a connection reference to the request?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Don Kaban, 2014-09-24
@donkaban

https://ru.wikipedia.org/wiki/Substitution_principle_...
Mandatory question in any C++ interview :)

S
Sergey, 2014-09-24
Protko @Fesor

class Connection {
    public: QueryResult execute(Query query);
}

T
Trrrrr, 2014-10-01
@Trrrrr

C++ allows you to implement things in a hundred ways. Therefore, it is better for yourself or the team to agree in advance what and how you are doing.
In fact, you can come up with a rule for yourself without any basic and distant reality principles like SOLID. In itself, inheritance in 90% of cases is necessary for the implementation of the interface. The remaining 10% fall into various C++ features. In some languages, there is no inheritance at all, but this does not prevent them from being OOP and writing beautifully.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question