2

I have just learned C++ for a little bit, and discover some special functions.

Example 1

bool operator<(const B& b1,const B& b2)
bool B::operator<(const B& b2) const
//recognized by std::sort

Example 2

MyIterator C::begin();
MyIterator begin(C& c);
//recognized by range-based loop

As far as I know, those function are specially recognized in C++.
(Furthermore, in each pair, they are somehow recognized in the same manner.)

Question

What are the list of all functions that are recognized as special?
In other words, is there any part in C++ (official) specification that summarizes the list of them + how special they are?

I believe that if I blindly code without this knowledge, I may make some silly mistake, especially when interact with std:: class.

Sorry for a not-so-sensible topic name, but I can't think of a better one.

4

1 回答 1

3

IMO,以下在 C++ 中特别处理:

  • main(), 有超过 1 个有效语法
  • 构造函数和析构函数
  • 某些特殊关键字,用作类似函数:4 个强制转换 ( static, dynamic, const, reinterpret) 和typeid
  • begin()&end()方法或函数在处理基于范围的for循环时被特殊处理
  • 各种重载运算符
  • 转换运算符,例如struct A { operator int (); };

可能缺少一些物品。不确定,如果所有东西都列在标准的某个地方,那几乎是不可能的。

但是,您对搞砸的恐惧namespace std是错误的。它确实包含一些标准功能,但除非你不这样做using namespace std,否则不用担心会搞砸。

于 2017-02-13T07:15:56.400 回答