zoukankan      html  css  js  c++  java
  • 关于左值的错误

     1 #include<iostream>
     2 #include<string> 
     3 using namespace std;
     4 
     5 class TextBlock{
     6 public:
     7     TextBlock(string x):text(x){
     8     }
     9     //operator[] for const 对象
    10     const char& operator[](std::size_t position) const
    11     { return text[position]; }
    12 
    13     //operator[] for non-const 对象
    14     char operator[](std::size_t position)
    15     { return text[position]; }
    16     
    17 private:
    18     std::string text;
    19 };
    20 
    21 int main(){
    22     TextBlock tb("hello");
    23     tb[0] = 'x';      //报错: lvalue required as left operand of assignment
    24         
    25     std::cout << tb[0] << endl;
    26     
    27     
    28     const TextBlock ctb("World!!");
    29     std::cout << ctb[0]; 
    30     
    31 } 

  • 相关阅读:
    c#剪切板操作
    eclipse mvn build error tips
    Redis Tips
    IntilliJ Idea 使用中的问题与解决方案
    mongo
    python
    SQL Relative
    sybase update
    run current vim file
    git
  • 原文地址:https://www.cnblogs.com/wsl96/p/13796691.html
Copyright © 2011-2022 走看看