Ilh*_*rić 7 c++ pointers decltype
有人可以向我解释为什么我不能做以下事情:
int* b = new int(5);
int* c = new decltype(*b)(5);
cout << *c << endl;
Run Code Online (Sandbox Code Playgroud)
抛出C464'int&':不能使用'new'来分配引用.我该怎么做这样的事情?我需要的是我发送的变量的derefferenced基类型.
这虽然有效
int* b = new int(5);
int** a = new int*(b);
decltype(*a) c = *a;
cout << *c<< endl;
Run Code Online (Sandbox Code Playgroud)
我理解上面的代码是如何工作的,但我如何使用new执行类似的操作呢?
Cor*_*mer 18
解除引用运算符*返回一个您无法使用的引用new.相反,你可以使用std::remove_pointer在<type_traits>
int* b = new int(5);
int* c = new std::remove_pointer<decltype(b)>::type(5);
std::cout << *c << std::endl;
Run Code Online (Sandbox Code Playgroud)