#include <iostream>

class A
{
private:
    std::string a;
public:
    A(std::string b) :a(b){}
    const char& operator[](int b)const //两个const都不能少
    {
        return a[b];
    }
};
int main()
{
    A a("hello");
  //a[0]='j'; 不能
char*p = &a[0];
  *p = 'j'; 也不能,编译器信息:“return”: 无法从“const char”转换为“char &” std::cout
<< *p; }

 

相关文章:

  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
猜你喜欢
  • 2022-12-23
  • 2021-06-06
  • 2021-06-12
  • 2021-04-24
  • 2021-04-23
  • 2022-12-23
相关资源
相似解决方案