对于这个计划
#include <iostream>
using std::cout;
struct C
{
C() { cout << "Default C called!\n"; }
C(const C &rhs) { cout << "CC called!\n"; }
};
const C f()
{
cout << "Entered f()!\n";
return C();
}
int main()
{
C a = f();
C b = a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Entered f()!
Default C called!
CC called!
Run Code Online (Sandbox Code Playgroud)
由于f()按值返回,它应该返回一个临时值.由于T a = x;是T a(x);,是不是要求建设的拷贝构造函数a,使用临时传入作为它的参数?
c++ variable-assignment copy-constructor compiler-optimization temporaries
为了分配动态内存,我一直在C++中使用向量.但是最近,在阅读一些源代码时,我发现使用"new int [size]"并在一些研究中发现它也分配了动态内存.
谁能给我建议哪个更好?我从算法和ICPC的角度来看?