相关疑难解决方法(0)

"const T&arg"与"T arg"

以下哪个示例是声明以下功能的更好方法?为什么?

void myFunction (const int &myArgument);
Run Code Online (Sandbox Code Playgroud)

要么

void myFunction (int myArgument);
Run Code Online (Sandbox Code Playgroud)

c++

17
推荐指数
5
解决办法
5950
查看次数

为什么复制ctor在此代码中使用?

class A
{
 public:
  A(const int n_);
  A(const A& that_);
  A& operator=(const A& that_);
};

A::A(const int n_)
{ cout << "A::A(int), n_=" << n_ << endl; }

A::A(const A& that_)    // This is line 21
{ cout << "A::A(const A&)" << endl; }

A& A::operator=(const A& that_)
{ cout << "A::operator=(const A&)" << endl; }

int foo(const A& a_)
{ return 20; }

int main()
{
  A a(foo(A(10)));    // This is line 38
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

执行此代码给出了o/p:

A :: …

c++ copy-constructor

10
推荐指数
2
解决办法
686
查看次数

标签 统计

c++ ×2

copy-constructor ×1