我必须这么做:
rentService(T service)方法应该注册租用服务的人的姓名。T (正在租用的服务)是Car,那么rentService也应该验证此人是否有有效的驾驶执照。我不想让你给我写代码,但是帮我提一些我应该如何处理的想法。我正在考虑用一个人的向量创建一个模板类Car。所以,我将通过向量的人和首先与驾驶执照出租它.But,我应该申报执照在哪里?还是创造一个阶级的人?
发布于 2020-05-18 16:49:33
您可以使用类型,它看起来像这样:
template<typename Event_Type>
class Renting
{
bool rentService(T service)
{
if (typeid(service) == typeid(Car))
{
checkDriverLicense()
{
}
}基于C++注释的Jarod42 17版本
bool rentService(T service)
{
if constexpr(std::is_same<Car, T>::value)
{
checkDriverLicense()
{
}或者,您可以使用模板专门化,如@keyoted建议。
template<typename Event_Type>
class Renting
{
bool rentService(Event_Type service);
};
template<typename Event_Type>
bool Renting<Event_Type>::rentService(Event_Type service)
{
// etc...
}
template<>
bool Renting<Car>::rentService(Car service)
{
// This will only be callet when Event_Type == Car
checkDriverLicense()
}发布于 2020-05-18 17:23:11
@yaodav的另一种选择是
template<typename Event_Type>
class Renting
{
bool rentService(Event_Type service);
};
template<typename Event_Type>
bool Renting<Event_Type>::rentService(Event_Type service)
{
// etc...
}
template<>
bool Renting<Car>::rentService(Car service)
{
// This will only be callet when Event_Type == Car
checkDriverLicense()
}https://stackoverflow.com/questions/61874575
复制相似问题