menu

hjk41的日志

Avatar

Effective C++ Note 27: Explicitly disallow use of implicitly generated member functions...

Explicitly disallow use of implicitly generated member functions you don't want

Take operator= for example. Sometimes we don't want people to call operator=, how to do that?
For other member functions, you just don't put it in the class, and no one could call it. However, for member functions like operator= and copy constructors, the compiler will implicitly generate one for you if you don't write one yourself.
You can make operator= private, but it won't prevent member and friend functions from calling it.
What if we make the member function abstract? Well, that will make the class abstract, and no instance can be created of an abstract class.
The answer is, to declare them, but not to define them.

评论已关闭