Advertisement

C++ auto C++ auto

阅读量:

事实上,在长期使用C++编程的过程中,发现C++11版本中新增的auto关键字具有较高的实用性。然而,此前在面对auto是否能够保留变量原有的const特性以及引用(&)属性的问题时,一时难以给出确切的答案。因此,编写了一小段测试程序以验证相关结论:auto关键字无法确保保留上述提及的属性,具体测试代码如下:

复制代码
 #include <iostream>

    
 #include <string>
    
  
    
 using namespace std;
    
  
    
 class Test
    
 {
    
 public:
    
     Test()
    
     {
    
  
    
     }
    
  
    
 public:
    
     const std::string& getString()
    
     {
    
     return str;
    
     }
    
  
    
 public:
    
     std::string str;
    
 };
    
  
    
 int main()
    
 {
    
     Test test;
    
     test.str = "abc";
    
     std

全部评论 (0)

还没有任何评论哟~