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 7308281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:32:09+00:00 2026-05-28T23:32:09+00:00

Timer.h: template<class T> class Timer { public: typedef T Units; virtual Units get() =

  • 0

Timer.h:

template<class T>
class Timer {
    public:
      typedef T Units;

    virtual Units get() = 0;
};

TimerImpl.h:

class TimerImpl: public Timer<long> {
public:

TimerImpl() {
}

~TimerImpl() {

}

long get();

};

FpsMeter.h(version 1):

template <class T> 
class FpsMeter {
    private:
    Timer<T>* timer;

    public:
    FpsMeter (Timer<T>* timer) {
        this->timer = timer;
    }

    ...

};

This example works. But it does not look pretty.

Timer<long>* t = new TimerImpl();
FpsMeter<long>* f1  = new FpsMeter<long> (t);

Here there are a lot of extra template uses. How can I realize this idea of multy-type interface when the type is defined by the implementation and user class has not to define new type, it should use the type of the implementation.

  • 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-28T23:32:10+00:00Added an answer on May 28, 2026 at 11:32 pm

    If you don’t mind a helper template function which always creates FpsMeter on the heap you could something like the following

    template < class T >
    FpsMeter<T> *make_FpsMeter( Timer<T> *timer ) {
      return new FpsMeter<T>( timer );
      }
    

    Then creating a FpsMeter of the appropriate type is like so

    FpsMeter<long> *f1 = make_FpsMeter( new TimerImpl() );
    

    Or if you can use C++11 auto you’d get

    auto f1 = make_FpsMeter( new TimerImpl() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Interface: template <class T> class Interface{ public: typedef T Units; virtual T get() =
Consider the following template class class MyClassInterface { public: virtual double foo(double) = 0;
Consider this code: template <typename T> class String { public: ... String(T* initStr) {
I'm writing a template class which should take an array as its type. public
I'm trying to create my own class template for Visual Studio called Public Class.
I'm told that the template system in C++ is Turing-complete at compile time. This
any time i write this: $this->setTemplate('module/action'); I'm getting this error: The template module/actionSuccess.php does
function Timer() { this.initialTime = 0; this.timeStart = null; this.getTotalTime = function() { timeEnd
Imagine the following template class (setter and getter for the member _t omitted): template<class
I want to specialize a class template with the following function: template <typename T>

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.