如何搜索c++代码中的所有构造函数?

问题描述 投票:0回答:3

我必须找出我的代码库(很大)中的所有构造函数,有没有简单的方法可以做到这一点(无需打开每个文件,读取它并查找所有类)?我可以在 grep 中使用任何特定于语言的功能吗?

要找到析构函数很容易,我可以搜索“~”。 我可以编写一些代码来查找“::”并匹配左右单词,如果它们相等,那么我可以打印该行。 但如果构造函数位于类内部(在 H/HPP 文件中),则缺少上述逻辑。

c++ constructor
3个回答
1
投票

既然您正在考虑使用 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!

我没有测试它,我做得很快,并试图简化旧的代码,所以很多东西不支持,比如嵌套类。 由此,您可以编写一个脚本来查找目录中的每个标头,并按照您喜欢的方式使用结果!


0
投票

搜索所有类名,然后找到与类名同名的函数。第二个选项是,我们知道构造函数始终是公共的,因此搜索单词 public 并找到构造函数。


0
投票

查找构造函数相当简单(正如其他人所说)...查找对构造函数和析构函数的所有调用并非易事,到目前为止我还没有找到......

© www.soinside.com 2019 - 2024. All rights reserved.