相关疑难解决方法(0)

引用成员变量作为类成员

在我的工作地点,我看到这种风格被广泛使用: -

#include <iostream>

using namespace std;

class A
{
public:
   A(int& thing) : m_thing(thing) {}
   void printit() { cout << m_thing << endl; }

protected:
   const int& m_thing; //usually would be more complex object
};


int main(int argc, char* argv[])
{
   int myint = 5;
   A myA(myint);
   myA.printit();
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

有没有名称来形容这个成语?我假设它是为了防止复制大型复杂对象的可能大量开销?

这通常是好的做法吗?这种方法有什么缺陷吗?

c++ reference

58
推荐指数
3
解决办法
7万
查看次数

标签 统计

c++ ×1

reference ×1