随笔-59  评论-36  文章-0  trackbacks-0
此刻我本已在床上睡觉了,但就在我睡前翻看《深度探索C++对象模型》时我看到了一段内容,也就是这段内容使得我写下了这篇笔记。

这段内容大致讲的是关于模板中的命名决议:“scope of the template definition”与“scopy of the template instantition”

坦白的说我是第一次看到这一知识点,在上机验证后又回想《C++ Primer》 4th中可曾有提及过,后来发现4th中没有,倒是3rd中有专门的一小节专门陈述了下。

后来我在《深度探索C++对象模型》书中关于模板中命名决议内容的旁边写下了这样一段话:

             今天才发现还有这么个东西。我只能承认自己的无知与C++的变态,面对这一知识点。
                                                                                     2012.5.2 夜



posted on 2012-05-03 00:12 zhaoyg 阅读(432) 评论(1)  编辑 收藏 引用 所属分类: C/C++学习笔记

评论:
# re: 我的无知与C++的变态 2012-06-15 17:19 | i4
您好,看了您这篇blog,感觉您写的测试例子结果应该和书上结果一样。但我自己也写了一个,结果是错的。麻烦您看下:
首先是一个模板类声明的.h文件,内容为:

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 : 定义控制台应用程序的入口点。
//

#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;
}
但测试结果显示输出的是1,而不是5。这和书上结果不符合,不知道哪里有错?
如能给上您的测试代码,不胜感激!
小弟email:shaohaizhu88@126.com  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理