C++ 上奇怪的隐式转换

strange implicit conversion on C++

我对意外转换有疑问:

class BadString
{
 public:
    BadString(char const*);
    ...
    char& operator[] (size_t);            //(1)
    char const& operator[] (size_t);

    operator char* ();                    //(2)
    operator char const* ();

};

int main()
{
    BadString str("correkt");
    str[5] = 'c';  //possibly an overload resolution ambiguity !!!
}

解释是这样的:

The subscript operator at (1) seems like a perfect match. However is not quite perfect because the argument 5 has type int, and the operator expects size_t (unsigned int or unsigned long, but never int). Still, a simple standard integer conversion makes (1) easily viable. However, there is another candidate: the built-in subscript operator. Indeed, if we apply the conversion operator (2) to str, we obtain a pointer type, and now the the built-in subscript operator applies. This operator takes a ptrdiff_t argument, which on many platforms is equivalent to int.

我明白了(1)的原始参数不匹配,然后会发生隐式转换,但我不明白为什么编译器会尝试使用(2)转换str。

谢谢。

如果我 'expand' 你的代码是这样的,并使第二个下标运算符方法为常量,那么结果将是

#include <iostream>
#include <cstring>

class BadString {
  public:
      char s[1024];
      BadString(const char* s_)  {
          strncpy(s, s_, strlen(s_) + 1);
      }
      char& operator[](size_t pos) {
          return s[pos];
      }
      const char& operator[](size_t pos) const {
          return s[pos];
      }

      operator char*() {
          std::cout << "operator char*\n";
      }

      operator const char*() {
          std::cout << "operator const char*\n";
      }
};

int main() {
    BadString bs("correkt");
    bs[5] = 'c';
    std::cout << bs.s << "\n";
}

然后编译它运行,它按预期工作,输出只有一行

correct

发生(2) 隐式转换运算符等事实意味着您没有提供有关BadString class 的完整信息。也许你应该这样做。

而且,当然,如果它像提供的示例中那样实现,则没有任何理由 调用 (2) 隐式转换运算符。

UPD: 考虑到您在评论中提到的内容,我想目的是 operator const char*() 显式 转换如

(const char*)bs

返回指向数组 s 的第一个元素的指针,bs 正在保存。也就是说,当然, 不正确,有点难看,但这是我看到的唯一选择。