社区
工具平台和程序库
帖子详情
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.
...全文
92
8
打赏
收藏
微信扫一扫
点击复制链接
分享
下载分享明细
分享
举报
写回复
8 条
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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*);
};
打赏
举报
回复
赞
相关推荐
H
ow
to
Use Ghostscript
H
ow
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
pe
r file Choosing ....
H
ow
to
create an RPM package
This page describes in detail
h
ow
to
create an RPM package, and in particular,
h
ow
to
create a S
PE
C file. Unlike other RPM guides, this page explai
ns
the
s
pe
cifi
cs for F
ed
ora
with
links ...
H
ow
To
: Export C++ classes from a DLL
Both the DLL and the client code must be compil
ed
with
the
sam
e
version of MS Visual C++ in order
to
e
ns
ure
that the naming decoration scheme matches between the caller and the callee. Here is an ...
Introduction
to
the Standard
Template
Library
转载自:... The Standard
Template
Library, or STL, is a C++ library of container classes, algorithms, and itera
to
rs; it provides many of the basic algo
Wind
ow
s 10远程桌面服务配置文件rdpwrap.10.0.19041.1741.zip
Wind
ow
s远程桌面服务RDPWrap配置文件,适用于Wind
ow
s 10 10.0.19041.1741及之前部分版本。 此配置文件只能支持Wind
ow
s 10 10.0.19041.1741及之前部分版本,由于Wind
ow
s不断更新版本,可能会导致rdpwrap配置文件不支持最新的Wind
ow
s版本,请谨慎下载。 本账号会不定期更新支持最新Wind
ow
s 10版本的rdpwrap配置文件,高于10.0.19041.1741版本的Wind
ow
s 10用户,请加粉关注以获取最新的rdpwrap配置文件。
发帖
工具平台和程序库
微信扫一扫
点击复制链接
分享社区
下载分享明细
2.4w+
社区成员
2.7w+
社区内容
C/C++ 工具平台和程序库
社区管理员
加入社区
帖子事件
创建了帖子
2002-12-30 03:53
社区公告
暂无公告