您好,看了您這篇blog,感覺您寫的測試例子結(jié)果應(yīng)該和書上結(jié)果一樣。但我自己也寫了一個,結(jié)果是錯的。麻煩您看下:
首先是一個模板類聲明的.h文件,內(nèi)容為:
extern double foo(double);
template <typename type>
class ScopeRules {
public:
type invariant()
{
return foo( _val );
}
type type_dependent()
{
return foo( _member );
}
type get() { return _member; }
private:
int _val;
type _member;
};
double foo(double d)
{
return 5.0;
}
然后是主程序文件:
// P289.cpp : 定義控制臺應(yīng)用程序的入口點。
//
#include "stdafx.h"
#include <iostream>
#include "template.h"
using namespace std;
extern int foo( int );
int _tmain(int argc, _TCHAR* argv[])
{
ScopeRules<int> sr0;
sr0.invariant();
cout << sr0.invariant() << endl;
cout << sr0.type_dependent() << endl;
return 0;
}
int foo(int i)
{
return 1;
}
但測試結(jié)果顯示輸出的是1,而不是5。這和書上結(jié)果不符合,不知道哪里有錯?
如能給上您的測試代碼,不勝感激!
小弟email:shaohaizhu88@126.com
回復(fù) 更多評論