2

I wonder if what I'm currently doing is a shame for C++, or if it is OK.

I work on a code for computational purpose. For some classes, I use a normal inheritance scheme with virtuality/polymorphism. But I need some classes to do intensive computation, and it would be great to avoid overhead due to virtuality.

Basically, I want to use this classes without pointers or redirection : inheritance is just here to avoid many copy/paste of code (the file size of the base class is like 60Ko (which is a lot of code)). So no virtual functions, and no virtual desctructor.

I wonder if it is perfectly OK from a C++ point of view or if it can create side effects (the concerned classes will be used a lot in the program).

Thank you very much.

Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 5
    It is ok, but virtual methods don't really have much overhead. If your "intensive computation" doesn't involve a ridiculous number of virtual method calls then it really doesn't matter. – robert May 23 '12 at 13:04
  • 5
    If you don't want to override anything, maybe composition instead of inheritance is better? – Tadeusz Kopec for Ukraine May 23 '12 at 13:05
  • 1
    Even if there are virtual methods the compiler might well be able to optimise them to ordinary function calls if the type of object is known (e.g. when you're working on a stack-allocated instance of the derived class). – leftaroundabout May 23 '12 at 13:08

6 Answers6

5

Using polymorphism in C++ is neither good nor bad. Polymorphism serves a purpose, as does a lack of polymorphism. There is nothing wrong with using inheritance without using polymorphism on its own.

Since polymorphism serves a purpose, and the lack of polymorphism also serves a purpose, you should design your classes with those purposes in mind. If, for example, you need runtime binding of behavior to class instances, you need polymorphism.

That all being said, there are right and wrong reasons for choosing one approach over the other. If you are designing your classes without polymorphism strictly because you want to "avoid overhead" that is likely a wrong reason. This is an instance of premature optimization so long as you are making design changes or decisions without having profiled your code and proved that polymorphism is an actual problem.

Design by architectural requirements first. Later go back and refactor if the design proves to be non-performant.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
4

I would rephrase the question:

What does inheritance brings that composition could not achieve if you eschew polymorphism ?

If the answer is nothing, which I suspect, then perhaps that inheritance is not required in the first place.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • 1
    +1 I think your suspicions might be correct. If the goal is to prevent cut'n'pasting code, composition should be preferred in many circumstances. – daramarak May 23 '12 at 13:38
3

Not using virtual members/inheritance is perfectly ok. C++ is designed to entertain vast audience and it doesn't restrict anyone to particular paradigm.

You can use C++ to code procedural, generic, object-oriented or any mix of them. Just try to make best out of it.

I'm currently doing is a shame for C++, or if it is OK.

Not at all.
Rather if you don't need OO design and still imposing it just for the sake of it, would be a shame.

Basically, I want to use this classes without pointers or redirection ...

In fact you are going in right direction. Using pointers, arrays and such low level features are better suited for advance programming. Use instead like std::shared_ptr, std::vector, and standard library containers.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 2
    I don't think he means *virtual inheritance* but rather the use or lack of *virtual* member functions in the inheritance tree. In the same way, when he says *without* pointers or redirection, I believe he means only *auto* allocated objects. – David Rodríguez - dribeas May 23 '12 at 13:37
  • @DavidRodríguez-dribeas, corrected the first part. Also in a way, containers like `shared_ptr/vector` are also auto allocated objects only. – iammilind May 23 '12 at 14:06
  • The `shared_ptr` is allocated in the stack, but the actual pointed object is not. The same goes for a vector, the vector object is in the stack, the memory is in the heap – David Rodríguez - dribeas May 23 '12 at 14:31
1

Basically, you are using inheritance without polymorphism. And that's ok.

Object-oriented programming has other feature than polymorphism. If you can benefits from them, just use them.

Mesop
  • 5,187
  • 4
  • 25
  • 42
1

In general, it is not a good idea to use inheritance to reuse code. Inheritance is rather to be used by code that was designed to use your base class. I would suggest a different approach to the problem. Consider some of the alternatives, like composition, changing the functionality to be implemented in free functions rather than a base class, or static polymorphism (through the use of templates).

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

It's not a performance problem until you can prove it.

Check out that answer and the "Fastest possible delegates" article.

Community
  • 1
  • 1
Vladimir Sinenko
  • 4,629
  • 1
  • 27
  • 39