Warning: Unreferenced local variable
When I provide a constructor for class A, I don't get the unreferenced local variable why? What does the empt开发者_运维技巧y constructor do to eliminate the warning?
class A
{
public:
A() {}
};
int main()
{
A a;
}
This is only a theory, but because a constructor may contain code that can cause side effects, someone may decide to construct an unused object just to run that code. If you have no constructor and never reference an object that you've constructed, then it can safely be determined that the object has no purpose.
For example if A
is something that holds a mutex lock (and release the lock when destructed), then this code
int main()
{
A a;
// other actions
}
is able to keep this function thread-safe, even a
does not be referenced.
精彩评论