Virtual function question

Started by
10 comments, last by Trienco 12 years ago
Even if it was doable in a clean manner, I don't like the idea of a parent class transparently intercepting a call to a child class's virtual method. You may have it clear in your head now, but if you ever had to revisit the code some months down the road, there would be no indication in the child class's method that it's parent class might intercept the call. Imagine trying to debug that. Calling Parent::Method() at the top of Child::Method() and checking the result to possibly return early tells you exactly what's going on when you're looking at the code.
Advertisement

Even if it was doable in a clean manner, I don't like the idea of a parent class transparently intercepting a call to a child class's virtual method.


If you are talking about NVI, that is at no point intercepting a virtual function call. What it DOES is giving you a single place if you want to add logging or pre-/post processing steps. Using NVI is advice I followed once (out of principle without really "needing" it at the time), having all "createMessage" calls go to the base class which THEN calls a virtual doCreateMessage (with a default implementation in base).

Want to guess how glad I was about that, when I realized I need to add important steps before and after (for example checking parameters or in this case, patching in size and crc info). The tiny overhead of that indirection is nothing compared to copy/pasting code to over a dozen derived classes. It also allowed me to log the message data in a single place instead of copying THAT code to a ton of classes/files as well. Not to mention you always know where to place that breakpoint...

Obviously that won't be helping, if it's not really something that needs to be done always and in every case.
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement