简体   繁体   English

如何知道c ++中的函数是否在类中未使用?

[英]How to know if function in c++ are unused if they are in a class?

Is there a method to know if a private function isn't used within the definition class? 有没有一种方法可以知道定义类中是否未使用私有函数? Also, I need to know if there some public function of the class which is not used outside the class. 另外,我需要知道该类是否有一些未在类外部使用的公共函数。 How can achieve this? 如何实现呢?

The solution depends heavily on the situation. 解决方案在很大程度上取决于情况。

In many cases, you can simply grep or ag the source for calls to the function. 在许多情况下,您可以简单地grepag调用函数的源。 Also, there are tools like cppcheck that can analyze a source base and tell you this. 此外,还有诸如cppcheck类的工具可以分析源库并告诉您这一点。

However, if you don't have source then there are other methods. 但是,如果您没有源,则还有其他方法。

Add code to the functions that will output somehow visibly (perhaps to a logfile) when called. 将代码添加到将在调用时以某种方式明显输出(也许输出到日志文件)的函数中。 This is what gcc's Code Coverage functions do. 这就是gcc的代码覆盖功能。

Remove the function and see whether your project still compiles and links. 删除该函数,然后查看您的项目是否仍在编译和链接。

You should remove the definition (in the cpp file) only, and leave the declaration in the header. 您仅应删除定义(在cpp文件中),并将声明保留在标题中。 Otherwise overload resolution could mask the places where your function is called. 否则,重载分辨率可能会掩盖调用函数的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM