由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个C++ template的问题
相关主题
这次Go丢人有点大呀为什么我的visual C++ 找不到 "Stdafx.h" ?
Java 8 闪亮发布了effective C++里的memory pool 一问:
C++问题几个Re: 110道C++面试题目,你会做多少? (转载)
发现还没写过IInterface {....}求助: 关于用VC做user interface
用C设计Stack的interface,要求支持各种数据类型。 (转载)how to create an interface file in a Visual C++ project
static polymorphism一问[菜鸟问问题]请问如果有两个 C++ library
一个C#似疑模板问题qeustion about separation of interface and implementation in java
请教matlab转C++请教c++ interface class问题
相关话题的讨论汇总
话题: template话题: iidbase话题: c++话题: class话题: interface
进入Programming版参与讨论
1 (共1页)
z****p
发帖数: 138
1
我在写一个generic 的函数比较两个class是否相等, 其中一个相等条件是这两个
class的ID 相同,可不可以在template里要求传入的类型必须有ID这个member
variable呢?怎样才能做得到呢?
谢谢
c*******a
发帖数: 18
2
one way i can think is let every class inheredited from
a base class with id variable in it, and in your function
you use base class to do comparison

【在 z****p 的大作中提到】
: 我在写一个generic 的函数比较两个class是否相等, 其中一个相等条件是这两个
: class的ID 相同,可不可以在template里要求传入的类型必须有ID这个member
: variable呢?怎样才能做得到呢?
: 谢谢

z****p
发帖数: 138
3
Yes, this is one solution. However, the project is close to the end. The
design change is impractical at this stage.
Thanks.

【在 c*******a 的大作中提到】
: one way i can think is let every class inheredited from
: a base class with id variable in it, and in your function
: you use base class to do comparison

c*****t
发帖数: 1879
4
It is a template... You just assume the functions / data members exist.
It is the responsibility of the classes this template uses to have
the functions / data member.

【在 z****p 的大作中提到】
: 我在写一个generic 的函数比较两个class是否相等, 其中一个相等条件是这两个
: class的ID 相同,可不可以在template里要求传入的类型必须有ID这个member
: variable呢?怎样才能做得到呢?
: 谢谢

z***e
发帖数: 14
5
1. You can dynamic-cast the in object to target types. If it fails, do
nothing. Otherwise, get the id from the casted type.
2. If there is no target type known-in-advance, for example you don't want
to list all possible target types or it is created by third party, you can
use template constrain in definition. Some thing like this:
bool Comparer (T t1, T t2) where T : IIDBase
{
return t1.Equals(t2);
}
IIDBase is the interface:
Interface IIDBase
{
int GetID();
}
Then derive a

【在 z****p 的大作中提到】
: 我在写一个generic 的函数比较两个class是否相等, 其中一个相等条件是这两个
: class的ID 相同,可不可以在template里要求传入的类型必须有ID这个member
: variable呢?怎样才能做得到呢?
: 谢谢

z****p
发帖数: 138
6
Thanks, your second solution is exactly what I am looking for. I remember
somewhere I saw this kind of specification, but forget it.

【在 z***e 的大作中提到】
: 1. You can dynamic-cast the in object to target types. If it fails, do
: nothing. Otherwise, get the id from the casted type.
: 2. If there is no target type known-in-advance, for example you don't want
: to list all possible target types or it is created by third party, you can
: use template constrain in definition. Some thing like this:
: bool Comparer (T t1, T t2) where T : IIDBase
: {
: return t1.Equals(t2);
: }
: IIDBase is the interface:

t****t
发帖数: 6806
7
what about just use
std::tr1::is_same::value

【在 z****p 的大作中提到】
: 我在写一个generic 的函数比较两个class是否相等, 其中一个相等条件是这两个
: class的ID 相同,可不可以在template里要求传入的类型必须有ID这个member
: variable呢?怎样才能做得到呢?
: 谢谢

1 (共1页)
进入Programming版参与讨论
相关主题
请教c++ interface class问题用C设计Stack的interface,要求支持各种数据类型。 (转载)
问个弱问题,C++为什么把interface和implementation分开?static polymorphism一问
COM本质论调用DLL里面构造函数的一点不解一个C#似疑模板问题
C++ 普及课程 (视频):Vector vs Deque请教matlab转C++
这次Go丢人有点大呀为什么我的visual C++ 找不到 "Stdafx.h" ?
Java 8 闪亮发布了effective C++里的memory pool 一问:
C++问题几个Re: 110道C++面试题目,你会做多少? (转载)
发现还没写过IInterface {....}求助: 关于用VC做user interface
相关话题的讨论汇总
话题: template话题: iidbase话题: c++话题: class话题: interface