如何在C ++代码中搜索所有构造函数?
how to search for all constructors in c++ code?
我必须找出我的代码库中的所有构造函数(这是巨大的(,有没有简单的方法可以做到这一点(无需打开每个文件,读取它并找到所有类(?我可以在 grep 中使用任何特定于语言的功能?
要找到析构函数很容易,我可以搜索"~"。 我可以写一些代码来查找"::"并匹配左右单词,如果它们相等,那么我可以打印该行。 但是,如果构造函数在类内(在 H/HPP 文件中(,则缺少上述逻辑。
既然你正在考虑使用 grep,我假设你想以编程方式完成它,而不是在 IDE 中。 这也取决于您是解析标头还是代码,我再次假设您要解析标头。
我用python做到了:
inClass=False
className=""
motifClass=re.compile("class [a-zA-Z][a-zA-Z1-9_]*)")#to get the class name
motifEndClass=re.compile("};")#Not sure that'll work for every file
motifConstructor=re.compile("~?"+className+"(.*)")
res=[]
#assuming you already got the file loaded
for line in lines:
if not inClass:#we're searching to be in one
temp=line.match(class)
if temp:
className=res.group(1)
inClass=True
else:
temp=line.match(motifEndClass)
if temp:#doesn't end at the end of the class, since multiple class can be in a file
inClass=False
continue
temp=line.match(motifConstructor)
if temp:
res.append(line)#we're adding the line that matched
#do whatever you want with res here!
我没有测试它,我做得相当快,并试图简化一段旧代码,所以很多东西都不支持,比如嵌套类。 由此,您可以编写脚本来查找目录中的每个标头,并按照自己的喜好使用结果!
搜索所有类名,然后找到与类名相同的函数名称。第二种选择是,我们知道构造函数始终是公共的,因此搜索单词 public 并找到构造函数。
查找构造函数相当简单(正如其他人所说(....查找对构造函数和析构函数的所有调用并非易事,到目前为止,我还没有找到......
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 当从函数参数中的临时值调用复制构造函数时
- 在c++构造函数中使用随机字符串生成器
- 一对向量构造函数:初始值设定项列表与显式构造
- 从构造函数抛出异常时如何克服内存泄漏
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 具有默认模板类型的默认构造函数的类型推导
- 如何在C ++代码中搜索所有构造函数?
- 为二进制搜索树副本构造函数编写辅助函数
- 二叉搜索树的复制构造函数被无限调用