相关疑难解决方法(0)

传递参考与传递值之间有什么区别?

有什么区别

  1. 通过引用传递的参数
  2. 一个由值传递的参数?

你能给我一些例子吗?

language-agnostic pass-by-reference pass-by-value

539
推荐指数
11
解决办法
65万
查看次数

引用成员变量作为类成员

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

#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万
查看次数