A warning. Inaccessible local variable

When I provide a constructor for class A, am I not getting a local variable without references? What does an empty constructor do to eliminate the warning?

class A { public: A() {} }; int main() { A a; } 
+4
source share
2 answers

This is just a theory, but since the constructor may contain code that can cause side effects, someone may decide to build an unused object just to run this code. If you do not have a constructor and never refer to the object you created, then you can safely determine that the object has no purpose.

+7
source

For example, if A is something that contains a mutex lock (and releases the lock upon destruction), then this code

 int main() { A a; // other actions } 

able to support this function in streaming mode, even A does not refer.

+1
source

All Articles