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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:54:53+00:00 2026-05-29T23:54:53+00:00

In C++ pure virtual classes are often used for runtime polymorphism. So you have:

  • 0

In C++ pure virtual classes are often used for runtime polymorphism.

So you have:

class IInterfaceA
{
    virtual void DoFoo() = 0;
};

And derived classes like:

class CFancyObject : public IInterfaceA
{
...

Which then can be used in functions like:

void Foo(IInterfaceA &interface);

But this is runtime case, and if the objects are known at compile time, we can do better by using CRTP:

template<class T> class IInterfaceA
{
public:
    void DoFoo()
    {    
        static_cast<T*>(this)->CallDerivedFunction();
    }
}

class CFancyObject : public IInterfaceA<CFancyObject>
{
    ...
}

Is it possible to use CRTP based derived classes in functions that take IInterface as a parameter?

void Foo(IInterfaceA<?> &interface);
  • 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-29T23:54:55+00:00Added an answer on May 29, 2026 at 11:54 pm

    An interface is meant to decouple the API of a class from its implementation. By introducing a template parameter you are tightly coupling the implementation to the interface, defeating the whole purpose. CRTP is meant to solve a different set of problems.

    If you make the interface templated, the function that takes it as a parameter must be templated as well. Once you’ve done that there’s no difference between using the interface class and using the implementation class.

    template<class T>
    void Foo(IInterfaceA<T> &interface) { interface.DoFoo(); }
    

    is identical to and provides no advantages over

    template<class T>
    void Foo(T &object) { object.DoFoo(); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following hierarchy of classes class classOne { virtual void abstractMethod() =
I have 2 classes: class Base { public: virtual int Foo(int n); virtual void
I have a base class MyBase that contains a pure virtual function: void PrintStartMessage()
I have an abstract class defining a pure virtual method in c++: class Base
I have an abstract class with a pure virtual function f() and i want
I have some abstract class called IClass (has pure virtual function). There are some
I have two classes, derived from a common class. The common class has a
I have a couple of pure virtual classes, Matrix and Vector . Throughout my
I have a base class class Animal with pure virtual functions, and a set
I have three different base classes: class BaseA { public: virtual int foo() =

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.