mercredi 16 avril 2014

polymorphisme - C++: héritage sans virtualité - Stack Overflow


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.




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.




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.




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.




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.




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




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


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



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.



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.



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.



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.



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.



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



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


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


0 commentaires:

Enregistrer un commentaire