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
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.
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
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: