模板与内存分配
#include <iostream>
template<class T> T CreateArray(T a, int n)
{
a = new T [n]; // mistake: double* = double**
return a;
}
int main()
{
double* a;
int n = 5;
a = CreateArray(a,n);
return 0;
}
我可以使用模板和新分配内存吗?我的错误是什么?
#include <iostream>
template<class T> T CreateArray(T a, int n)
{
a = new T [n]; // mistake: double* = double**
return a;
}
int main()
{
double* a;
int n = 5;
a = CreateArray(a,n);
return 0;
}
can I allocate memory using a template and new? And what my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的代码有一些错误的地方。首先,您可以做类似您想要做的事情,但您应该编写如下内容:
请注意,您不必传递
a
数组(它将被复制到>CreateArray
,其更改在main
内不可见)。另请注意,您定义了返回指针T*
的模板,这正是main()
a
所期望的。Your code has some wrong things. First, you can do something like what you're trying to do, but you should write something like this:
Note that you don't have to pass the
a
array (it will be copied insideCreateArray
, and its changes won't be visible insidemain
). Note also that you define the template to returning a pointerT*
, that is whatmain()
a
is expecting.因此其他人已经解释了为什么您的代码不起作用以及如何改进它。
现在我将展示如何仍然让以下代码进行编译并正常工作:
正如已经提到的,问题是 C++ 不会单独从返回类型推断模板参数。
您可以通过使上述函数返回一个简单的代理对象来规避此限制。代理对象只有一个操作:(隐式)转换为
T*
。这是实际分配发生的地方。因此,
CreateArray
函数非常简单(并且不是模板):至于代理:
简单如 π。
现在,您应该使用这段代码吗?不,可能不会。与直接分配相比,它没有提供真正的优势。但这是一个很有用的习语。
So others have explained why your code doesn’t work and how it can be improved.
Now I’ll show how you can still get the following code to compile – and to work properly:
The problem, as already mentioned, is that C++ does not infer template arguments from return types alone.
You can circumvent this limitation by making the above function return a simple proxy object. The proxy object has a single operation: an (implicit) conversion to
T*
. This is where the actual allocation happens.The
CreateArray
function is therefore very simple (and not a template):As for the proxy:
Easy as π.
Now, should you use this code? No, probably not. It offers no real advantage over direct allocation. But it’s a useful idiom to know.
您想要接受指向要分配的类型的指针:
这应该可以解决问题。
You want to accept a pointer to the type you want to allocate:
This should do the trick.
我更喜欢将空指针值保留为 NULL。
vector
也可能是一个很好的解决方案。我认为这是更好的一种,因为你不会造成内存泄漏。I prefer to keep empty pointers value NULL.
vector
could be a good solution, too. I think it is better one, because you won't make memory leak(s).