Home > Blogs > 30 C++ Tips in 30 Days: Tip # 20 avoid empty constructor definitions

30 C++ Tips in 30 Days: Tip # 20 avoid empty constructor definitions

By  Dec 8, 2007

Topics: Programming, C/C++

For a reason I can't fathom, many classes contain an empty constructor definition. The question isn't what it's good for, but rather, how bad is it.

When you define a class that doesn't need a user-define constructor, it's best to leave that class without a constructor definition:

Class Myclass
{
public:
 Myclass() {} //bad
};

The user-defined constructor (even if it's empty) is in most cases less efficient than an implicitly-defined constructor so there's no point in adding it to the class. The same rule applies to destructor as well, unless the class is meant to be a base class (in which case your destructor must be declared explicitly, and it must be virtual as well).

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.

Other Things You Might Like

Xamarin Unleashed

Xamarin Unleashed