CPPDepend Detection of virtual function usage

67 Views Asked by At

I'm having a slight problem with CPPDepend's ability to detect virtual function usage. Consider the following scenario. Two classes, CParentClass and CChildClass, where CChildClass is derived from CParentClass. The CParentClass has a virtual function Test and CChildClass overrides the base class version of Test.

When it comes to usage, for various reasons I want to do something like the following:-

CChildClass * pMyChild = new CChildClass();
CParentClass * pParentClass = (CParentClass*)pMyChild;
int B = pParentClass->Test();
delete pParentClass; 

This results in pMyChild's Test function being called, as desired, yet CPPDepend doesn't detect this and claims that the code is never reached. If I add the word "virtual" to the Test function header in CChildClass (in addition to the one already in CParentClass) then CPPDepend claims everything is ok.

Can anyone shed some light on this for me please as it feels wrong that I should have to put virtual in the derived class function as well as the base class function.

A similar issue can be seen with CDialog destructors in derived classes. Without the virtual in the derived class destructor declaration, CPPDepend complains.

Thanks for any help you can give.

Regards

Neil.

1

There are 1 best solutions below

0
On

CppDepend do a static analysis not a dynamic one, and give the dependencies from a static point of view and it's more interesting. Indeed what's important is the dependency related to the design choices, for example in your case the object is declared as CParentClass, so the method is coupled with the contract of CParentClass, and in the runtime it could invoke a method from child classes.