用 reference to const 作形参,类型不匹配时,发生类型转换,产生临时对象

269 阅读1分钟
  • reference to const参数,极可能会产生临时对象绑定到该参数上
    对下函数而言str是临时对象
    // ! 编译都通不过, 编译器拒绝做,既产生临时对象,又想把这个临时对象当作原对象的引用,这种蠢事
    // void use(std::string&) 
    void use(const std::string& str)
    {}  
    
    int main()
    {	
            char array[] = "hello world";
            use(array);
            return 0;
    }