在我工作的地方,我看到这种风格被广泛使用:-
#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;
}
这个成语有什么名字可以形容吗?我假设它是为了防止复制大型复杂对象可能带来的巨大开销?
这通常是好的做法吗? 这种方法有什么缺陷吗?