Andrey Chernukha
Andrey Chernukha

Reputation: 21808

How to make C++ compiler produce error or warning when an instance variable is uninitialized?

Consider this simple class

class A
{
 public:
    int a;
};

As you can see int a has never been initialized. I would like to get an error or at least warning in such cases. I'm using Xcode with GNU++11[-std=gnu++11] C++ dialect. Is it even possible?

UPDATE:

First of all I created an instance of A and used its a member and still received no warning:

A a;

std::cout << a.a;

Also I looked into Easy way to find initialized member variables, it suggests adding -Weffc++ flag. I tried to add it via custom compiler flags in the Xcode Build Settings (is it a wrong way to do so? I'm not sure) and it didn't work

Upvotes: 1

Views: 796

Answers (1)

P.W
P.W

Reputation: 26800

GCC provides -Wuninitialized compiler option for warning about uninitialized variables.

This works for your case. See demo here.

Upvotes: 1

Related Questions