0

If a variable's intended scope is truly local, it would only make sense to me to keep it that way. This is what I have always done. However I recently switched from vim to eclipse at work and eclipse is flagging my constructor if every member variable is not initialized in the constructor. For example, in the code below, it compiles just fine. g++ has no problem with it. Eclipse on the other hand tells me that 'a' should be initialized in the constructor.

Is there any reason why this warning might be relevant or is it just worth ignoring all together?

class C
{
public:
    C(){}
    ~C(){}
    void foo();
};

void C::foo()
{
    int a;
}

int main() {
    C c;
    return 0;
}
mreff555
  • 1,049
  • 1
  • 11
  • 21

1 Answers1

7

Eclipse on the other hand tells me that 'a' should be initialized in the constructor.

a cannot be initialized in the constructor because it's a local variable of the member function C::foo(). Eclipse is giving you a bogus warning.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • I suspect the OP posted a wrong example, rather than Eclipse giving a bogus warning. –  Oct 26 '17 at 16:03
  • Right. I figured it was trying to tell me to declare all variables outside the class member, but that is stupid and messy. – mreff555 Oct 26 '17 at 16:03
  • What is the OP? – mreff555 Oct 26 '17 at 16:04
  • 2
    @mreff555 OP = original poster, in this case you. Are you absolutely sure Eclipse is giving the message in your question for the code in your question, rather than for code that sort of looks like the code in your question? –  Oct 26 '17 at 16:06
  • 1
    Absolutely certain. Honestly, this isn't the first screwy thing I have seen eclipse flag. If it wasn't for the indexing I would have never considered switching. I just wanted to make sure there wasn't some strange common practice which I didn't know of. – mreff555 Oct 26 '17 at 16:08
  • @mreff555 Can you post a screenshot or a an error message? – Ron Oct 26 '17 at 16:20
  • @mreff555 Definitely no such common practice, the message doesn't make any sense for the code you posted. FWIW, Eclipse does not give me the warning for the code you posted, it only gives me the warning if I make `a` a member of the class. –  Oct 26 '17 at 16:32