简体   繁体   中英

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.

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.

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

Because this operator isn't a special member function . It's actually covered in this section

13.5 Overloaded operators
1 A function declaration having one of the following operator-function-ids as its name declares an operator function. A function template declaration having one of the following operator-function-ids as its name declares an operator function template. A specialization of an operator function template is also an operator function. An operator function is said to implement the operator named in its
operator-function-id.

 operator-function-id: operator operator operator: one of new delete new[] delete[] + - * / % ˆ & | ∼ ! = < > += -= *= /= %= ˆ= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> ( ) [ ] 

...

2 Both the unary and binary forms of + - * & can be overloaded.

If you would read the Standard more closely you find that special member functions are those functions that the compiler can declare implicitly if you will not declare them explicitly.

From the C++ Standard:

12 Special member functions [special] 1 The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions. [ Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are odr-used (3.2). See 12.1, 12.4 and 12.8. —end note ] An implicitly-declared special member function is declared at the closing } of the class-specifier. Programs shall not define implicitly-declared special member functions.

By the way the definition of the operator you showed is wrong becuase it returns nothing.

As for other member functions including operators that for example shall be declared as class members then the implementation does not declare them implicitly. It is the programmer who decides whether to declare some operators or not. For example your class may contain a dozen of assignment operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM