C ++:多态类模板 [英] C++: Polymorphic class template

查看:137
本文介绍了C ++:多态类模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个存储一堆Date对象的类Calendar。
日历用于保存从Date继承的任何类型对象的集合。我认为最好的方法是使用类模板,例如

Consider a class Calendar that stores a bunch of Date objects. The calendar is designed to hold a collection of any type of objects that inherit from Date. I thought the best way to do it is to have a class template such as

template<typename D> class Calendar{ 
    ...
}

但令我印象深刻的是D现在实际上可以是任何一个班级。
我现在的问题是,如何确保D是日期对象的子类?

But it struck me that D can now in fact be any class. My question is now, how can I make sure that D is a subclass of the date object?

我知道如何做到这一点是Java,但是我我仍然不熟悉C ++语法。问题非常类似于某些集合只能采用实现Comparable的模板变量。标题看起来像

I know how to do this is Java, but I'm still unfamiliar with the C++ syntax. The problem is very much similar to how some collections can only take a template variables that implement Comparable. The header would then look something like

public class Calendar<D extends Date>{
     ...
}

------------------- -EDIT:------------------------------------------

-------------------- ------------------------------------------

模板参数定义日历所指的实际日期。不同的日期类型以不同的格式表示同一天。例如,如果我创建日历< Gregorian> ,它将能够以另一个日期格式记录日期,比如说朱利安日历或任何其他日期格式,并以格里高利格式呈现。这样可以在不同日期格式的日历之间进行转换。所以,如果我有日历< Gregorian> ,我可以轻松地将其转换为日历< Julian> 。然后可能出现以下情况:

The template argument defines which actual day the calendar refers to. Different date types refer to the same day in different formats. For instance, if I make a Calendar<Gregorian> it will be able to take dates in another Date format, say the Julian calendar, or any other date format and present them in Gregorian format. This enables for conversion between calendars in different date formats. So, if I have a Calendar<Gregorian> I can easily convert it into a Calendar<Julian>. Then the following is possible:

Calendar<Gregorian> cal;
std::cout << "These events are entered as dates in 
    the Gregorian calendar" << std::endl;
cal.add_event("Christmas", 12, 25);
cal.add_event("Gregorian new year", 1, 1);
std::cout << cal << std::endl;
std::cout << "----" << std::endl;
std::cout << "And printed out as Julian dates" << std::endl;
Calendar<Julian>(cal);
std::cout << cal<< std::endl;

和输出:

These events are entered as dates in the Gregorian calendar
2009-12-25 Christmas
2010-01-01 Gregorian new year
----
And printed out as Julian dates
2009-12-13 Christmas
2009-12-19 Gregorian new year

-------------新编辑:----------------------

------------- New edit: ----------------------

现在最后一次编辑更有意义。我对格式略有不同意见。

The last edit now makes more sense. I had a slight disagreement with the formatting.

感谢所有答案。

我是电脑科学专业的第三年,我会说我对OO和相关概念如Polymorphism等非常熟悉。这篇文章的目的是找出C ++中是否有一种表达条件的方法。模板参数与Java相同,并以简洁,优雅和直观的方式解决问题。

I'm a Computer Science student on my third year, and I'd say I'm fairly familiar with OO and related concepts like Polymorphism etc. The purpose of this post was to find out whether or not there was a way in C++ to express a condition for a template argument the same way that it is in Java and solve the problem in a concise, elegant and intuitive way.

推荐答案


我知道如何做到这一点是Java,但我仍然不熟悉C ++语法。问题非常类似于某些集合只能采用实现Comparable的模板变量。然后标题看起来像

I know how to do this is Java, but I'm still unfamiliar with the C++ syntax. The problem is very much similar to how some collections can only take a template variables that implement Comparable. The header would then look something like



public class Calendar<D extends Date>{
     ...
}

是的,这是同样的问题,在C ++中,通常通过忽略它来解决。为什么我们需要强制对象必须实现 IComparable ?在Java中,由于其贫血型系统,它是必要的。没有这个约束,我们将无法比较对象。

True, it is the same problem, and in C++, it is usually solved by ignoring it. Why do we need to enforce that the object must implement IComparable? In Java, it's necessary because of its anemic type system. Without this constraint, we'd be unable to compare objects.

在C ++中,规则是不同的。容器只需尝试来比较它们存储的对象,如果类型不支持它,则会出现编译错误。不需要接口或继承。

In C++, the rules are different. Containers simply try to compare the objects they store, and if the type doesn't support it, you get a compile error. No interfaces or inheritance is required.

您通常在 Calendar 类中执行相同的操作。只需不强制执行必须子类表格日期约束。

And you'd typically do the same in your Calendar class. Simply don't enforce the "must subclass form Date constraint.

相反,指定类型必须公开的成员,以及应该从中获取什么语义。

Instead, specify the members the type must expose, and what, if any, semantics should be expected from them.

例如,如果您的日历尝试执行以下操作,日期对象 d0 d1

For example, if your Calendar attempts to do the following operations, for date objects d0 and d1:

d0.getDay();
d0.getTime();
Time t = d0 - d1;

然后这些是应该支持的操作。任何支持这些操作的类都是一个有效的Date类,即使它没有子类任何事情

Then those are the operations that should be supported. Any class which supports these operations is a valid Date class, even if it doesn't subclass anything.

这篇关于C ++:多态类模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆