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 to
Unique::Unique(Unique const&)' collect2: ld returned 1 exit status