0%

C++ 易错提醒

  • 不要返回局部对象的引用

    1
    2
    3
    4
    5
    std::string & func()
    {
    std::string temp = "hello";
    return temp; // Wrong!
    }
  • 可以返回类对象成员的引用,因为在对象存续期间,该成员也是存在的。

  • 引用其实是指针的语法糖