C ++: polymorphism pattern template

Consider a class calendar that stores a bunch of Date objects. A calendar is designed to store a collection of objects of any type that inherit from Date. I thought the best way to do this is to create a class template like

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

But it seemed to me that D can now be any class. Now my question is: how can I make sure that D is a subclass of the date object?

I know how to do this, this is Java, but I'm still not familiar with the C ++ syntax. The problem is very similar to how some collections can only accept template variables that implement Comparable. Then the headline would look like

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

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

, . . , Calendar<Gregorian>, Date, , . . , Calendar<Gregorian>, Calendar<Julian>. :

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

------------- : ----------------------

. .

.

, , OO , Polymorphism .. , ++, , Java, , .

+5
6

, , Java, ++. , , Comparable.

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

, , ++ , . , IComparable? Java - . .

++ . , , , . .

Calendar. "must subclate form Date.

, , , , .

, , d0 d1:

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

, . , , Date, -.

+9

, , - . ++, / .

, , , , , .

. , typedef :

class date {
  public:
    typedef int is_derived_from_date;
};

template<typename D> class Calendar{ 
    typedef typename D::is_derived_from_date blah;
    ...
};

- - is_derived<B,D>::result, , Calender. Boost - is_derived, .

, , . , ?

+8

, . D Date, Date?

+3

++ , . ++ , , . , , D Date, D Date, , - .. , D, Date, .

++ 0x, Boost.ConceptCheck ( Boost ).

, D Date, Boost.StaticAssert Boost.TypeTraits, , D Date.

+2

/. , .

template <typename T>
T clone(const T& cloneable) {
    return cloneable.create_clone();
} 

, create_clone(), ICloneable -interface!

, , .

, Date*.

, , Date*, , Date. , .

+1

Date, Date * -s?

, Date, , , , , .

, , ? (, ++ 0x, , , , , , .)

0

All Articles