【发布时间】:2016-12-29 11:59:39
【问题描述】:
我在 C++ 中有以下代码。每次我运行它,它都有不同的输出。为什么会这样?它是否与内存泄漏有关?
#include <iostream>
using namespace std;
template <class T, class U>
T f(T x, U y)
{
return x+y;
}
int f(int x, int y)
{
return x-y;
}
int main()
{
int *a=new int(4), b(16);
cout<<*f(a,b);
return 0;
}
【问题讨论】:
-
这与您尝试通过反复试验学习C++有关。这是行不通的。而是从一本好书中系统地学习它。
-
@JesperJuhl 谁说 f 返回一个 int? As a is int* 被注入 T?
-
@lordjohncena 我忽略了模板位 - 因此当我意识到时我删除了我的评论。
-
未定义的行为是未定义的。您实际上期望达到什么目标?