Search code examples
c++polymorphismvirtual

C++ : inheritance without virtuality


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.


Solution

  • 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.