Search code examples
c++c++11assignment-operatordefault-constructor

C++ Special member functions


I have always known that special member functions of C++ are:

  • Default constructor
  • Copy Constructor
  • Copy assignment operator
  • Destructor
  • Move constructor
  • Move assignment operator

Now I am reading Meyers Effective C++ book and have realized that there is also pair of address-of operators.

I can redefine it this way:

class A
{
public:
  A* operator&()
  {
    std::cout << "Address of operator" << std::endl;
  }
};

int main()
{
  A a;
  B* b = &a; // Will call address-of operator.
}

Why then in C++ standard section 12 (Special member functions) there is no word about this operator.


Solution

  • This should probably be an answer, not a comment, so be it:

    It's a mistake in your edition of Effective C++. The copy I have says:

    If you don't declare them yourself, your thoughtful compilers will declare their own versions of a copy constructor, an assignment operator, and a destructor.

    As you can see, there is no more mention of any address-of operator. The errata for the second edition explicitly mention this change:

    A class declaring no operator& function(s) does NOT have them implicitly declared. Rather, compilers use the built-in address-of operator whenever "&" is applied to an object of that type. This behavior, in turn, is technically not an application of a global operator& function. Rather, it is a use of a built-in operator.