社区
工具平台和程序库
帖子详情
How to ensure two template member functions specificated with same type?
noho
2002-12-30 03:53:04
class A
{
template<typename T> foo(T *);
template<typename T> bar(T *);
};
except for using RTTI technology.
Any suggestion are appreciated.
...全文
132
8
打赏
收藏
How to ensure two template member functions specificated with same type?
class A { template foo(T *); template bar(T *); }; except for using RTTI technology. Any suggestion are appreciated.
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
noho
2003-01-06
打赏
举报
回复
//nod
有人从理论上证明了compile-time check是不可能的。
结贴。
rushman
2003-01-04
打赏
举报
回复
--如果foo不是foo,而是class A的构造函数呢?
如果foo是class A的构造函数,那就需要将这个类进行封装(使用这个类的实例)。既然这样,为什么不把它设计成模板?如果需要,可以先设计一个虚基类,再派生出来这个模板。这样一般来说,可以实现与你的类相同的功能。
反过来,也说明你在类的设计上先天就有问题。有时候,不一定非要在墙上打个洞再钻过去,退一步,也许就会发现另有通途。^_^
noho
2003-01-04
打赏
举报
回复
如果foo不是foo,而是class A的构造函数呢?
rushman
2003-01-01
打赏
举报
回复
Happy new year..........
rushman
2003-01-01
打赏
举报
回复
既然能在编译期进行检查,就说明这两个函数的调用是由程序员安排的,所以我认为关键应该在于调用时的处理。
为了不至于在调用时产生混乱,应该将对这两个函数的调用部分封装起来。
首先,将这两个函数保护(protected)起来,然后,将调用这两个函数的代码放在一起,外部只能通过这些接口来访问。
//----------------------------------------------
另写一个模板函数当作跳板来调用这两个函数。
templact<class T>
void function(T * ){
foo(...);
bar(...);
}
//----------------------------------------------
把两个函数变成一个函数。从软件工程的角度,这是个馊主意。从面向对象的角度,这叫基于消息???^_^
template<class T>
void function(T*p,int msg)
{
switch(msg){
case FOO:....
case BAR:....
......
}
}
//---------------------------------------------
将这两个函数调用的接口设计成类,灵活性、封装和实用性更好。
template<class T>
class X{
public:
//使其界面象一个函数
void operator()(int msg,T*)
{
switch(msg){
case FOO:....
case BAR:....
......
}
}
};
noho
2002-12-31
打赏
举报
回复
我找到了一个方法,可惜是在运行时检查,不知道哪位能提供编译时检查的版本。
template<typename T>
struct TypeInfo
{
static void constraints(void){};
};
class A
{
template<typename T> foo(T *)
{
constraints = &TypeInfo<T>::constraints;
}
template<typename T> bar(T *)
{
assert(constraints == &TypeInfo<T>::constraints);
}
void (*constraints)();
};
foo一定先于bar调用。
qing_li73
2002-12-31
打赏
举报
回复
concern
zfluo
2002-12-31
打赏
举报
回复
template <class T>
class A
{
void foo(T*);
void bar(T*);
};
Google C++ Style Guide(Google C++编程规范)高清PDF
Another useful rule of thumb: it's typically not cost effective to inline
function
s with loops or switch statements (unless, in the common case, the loop or switch statement is never execut
ed
)....
Windows Store apps开发[43]C++/CX Part 4 of [n]: Static
Member
Function
s
更多内容请查看下面的帖子 [DevDiv原创]Windows 8 开发Step by Step ...C++/CX Part 4 of [n]: Static
Member
Function
s See C++/CX Part 0 of [n]: An Introduction for an introduction
项目启动会_How
项目启动会_How
How to Use Ghostscript
How to Use Ghostscript Table of contents Invoking Ghostscript Help at the command line:gs -h Selecting an output device Output resolution Output to files One page per file Choosing ....
How to Use Instruments in Xcode
转载自:http://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode At this point in your iOS development career, you’ve probably written an app or two, and you are no doubt wondering...
工具平台和程序库
24,854
社区成员
27,343
社区内容
发帖
与我相关
我的任务
工具平台和程序库
C/C++ 工具平台和程序库
复制链接
扫一扫
分享
社区描述
C/C++ 工具平台和程序库
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章