Search code examples
c++constructoroperator-overloadingimplicit-conversionaccess-specifier

Disallow functionality automatically provided by C++ compilers


Scott Meyers in his book "Effective C++" says,

To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations.Then if somebody inadvertently calls one, they will get an error at link-time.

I tried to write a sample program for achieving what Scott was trying to explain. I could achieve the same even when the member function was declared public and given no implementation. The link-error occurred in my case also when i tried to initialize an object from another object. So i do not understand why Scott is emphasizing on the need of member function to be declared private?

My sample program is written below:

#include <iostream>

using namespace std;

class Unique
{
   private:
      int num;

   public:
      Unique(int x)
      {
         num  = x;
      }
      Unique(const Unique &obj);

      Unique& operator =(const Unique &obj);

      void disp(void)
      {
         cout << "num = " << num << "\n";
      }
};

int main()
{
   Unique ob1(3);
   Unique ob2(ob1);

   ob1.disp();
   ob2.disp();

   return 0;
}

I get the following link-error:

/tmp/ccXfqSqE.o(.text+0x135): In function main': : undefined reference toUnique::Unique(Unique const&)' collect2: ld returned 1 exit status


Solution

  • Compiler errors are clearer and happen earlier (this is more pronounced in big projects ccompiled from a lot of source files) than link errors. They are also easier to read most of the time. Declaring members private provokes compile errors and is therefore preferred over leaving members undefined to disallow them.

    As Baptiste notes, in C++11 there is the even better delete keyword available for disallowing compiler-generated members:

    class A
    {
         A(constA&) = delete; // copy construction is not allowed
    };