c++ - Easy way find uninitialized member variables -
i looking easy way find uninitialized class member variables.
finding them in either runtime or compile time ok.
currently have breakpoint in class constructor , examine member variables 1 one.
if use gcc can use -weffc++
flag, generates warnings when variable isn't initialized in member initialisation list. this:
class foo { int v; foo() {} };
leads to:
$ g++ -c -weffc++ foo.cpp -o foo.o foo.cpp: in constructor ‘foo::foo()’: foo.cpp:4: warning: ‘foo::v’ should initialized in member initialization list
one downside -weffc++
warn when variable has proper default constructor , initialisation wouldn't necessary. warn when initialize variable in constructor, not in member initialisation list. , warns on many other c++ style issues, such missing copy-constructors, might need clean code bit when want use -weffc++
on regular basis.
there bug causes give warning when using anonymous unions, can't work around other switching off warning, can done with:
#pragma gcc diagnostic ignored "-weffc++"
overall have found -weffc++
incredible useful in catching lots of common c++ mistakes.
Comments
Post a Comment