如何让g ++对未使用的成员变量发出警告 [英] How to get g++ to warn on unused member variables

查看:158
本文介绍了如何让g ++对未使用的成员变量发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

g ++为未使用的局部变量生成警告。是否可能有未使用的类成员变量和/或全局变量的g ++警告?

g++ generates warnings for unused local variables. Is it possible to have g++ warn for unused class member variables and/or global variables?

class Obj {
 public:
  Obj(int a, int b) : num1(a), num2(b) {}
  int addA(int i) {
    return i + num1;
  }

 private:
  int num1;
  int num2;
};

如何让g ++警告我 num2 未使用?

How do I get g++ to warn me that num2 is unused?

更新:
我目前正在编译:

UPDATE: I am currently compiling with:

g++ -Wall -Wextra -pedantic *.cc -o myprogram 


推荐答案

我不知道有这样的警告。另外,我会推测,它不存在的原因是因为它不能可靠地生成所有情况下,所以他们选择不花费努力使其工作的一些子集的情况。例如,如果类 friend 是库中的另一个函数,编译器将无法知道该库是否突变了任何特定的类属性。

I'm not aware of any such warning. Additionally I'll speculate that the reason it doesn't exist is because it can't be reliably generated in all cases, so they elected to not spend effort making it work for some subset of cases. For example, if the class friends another function that's in a library, the compiler would have no way of knowing if that library mutated any particular class attribute or not.

这篇关于如何让g ++对未使用的成员变量发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆