Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:05:34+00:00 2026-05-12T17:05:34+00:00

Consider a class Calendar that stores a bunch of Date objects. The calendar is

  • 0

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{ 
    ...
}

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?

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

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;

and outputs:

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.

Thanks for all the answers.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:05:34+00:00Added an answer on May 12, 2026 at 5:05 pm

    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>{
         ...
    }
    

    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.

    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.

    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.

    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;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a class that uses an external jar. The class processes objects of type
It may not be so obvious how respond_to? works in ruby. Consider that: class
Consider a class that's used to represent the results of a GroupBy() . The
A tweak on this question that I've run into. Consider: class A {}; class
Consider this class from the WinAPI: typedef struct tagRECT { LONG left; LONG top;
Consider: Class Client Class Project Class Ticket Class Reply Clients have a sub collection
Consider: class MyClass<T> where T : class { } In that case, the where
Consider a class that has some events. This event list is going to grow.
Let's consider a chain of objects like this: Earth->Continent->Country->City->name Let's also consider Earth.class has
Consider the class below that represents a Broker: public class Broker { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.