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

  • SEARCH
  • Home
  • 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 3424234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:27:00+00:00 2026-05-18T06:27:00+00:00

If you have something like this: #include <iostream> template<typename T> class A { public:

  • 0

If you have something like this:

#include <iostream>

template<typename T> class A
{
public:
    void func()
    {
        T::func();
    }
};

class B : public A<B>
{
public:
    virtual void func()
    {
        std::cout << "into func";
    }
};

class C : public B
{
};

int main()
{
  C c;
  c.func();

  return 0;
}

Is func() dynamically dispatched?
How could you implement class A such that if B has a virtual override, that it is dynamically dispatched, but statically dispatched if B doesn’t?

Edit: My code didn’t compile? Sorry guys. I’m kinda ill right now. My new code also doesn’t compile, but that’s part of the question. Also, this question is for me, not the faq.

#include <iostream>

template<typename T> class A
{
public:
    void func()
    {
        T::func();
    }
};

class B : public A<B>
{
public:
    virtual void func()
    {
        std::cout << "in B::func()\n";
    }
};

class C : public B
{
public:
    virtual void func() {
        std::cout << "in C::func()\n";
    }
};
class D : public A<D> {
    void func() {
        std::cout << "in D::func()\n";
    }
};
class E : public D {
    void func() {
        std::cout << "in E::func()\n";
    }
};

int main()
{
  C c;
  c.func();
  A<B>& ref = c;
  ref.func(); // Invokes dynamic lookup, as B declared itself virtual
  A<D>* ptr = new E;
  ptr->func(); // Calls D::func statically as D did not declare itself virtual
  std::cin.get();

  return 0;
}

visual studio 2010\projects\temp\temp\main.cpp(8): error C2352: 'B::func' : illegal call of non-static member function
      visual studio 2010\projects\temp\temp\main.cpp(15) : see declaration of 'B::func'
      visual studio 2010\projects\temp\temp\main.cpp(7) : while compiling class template member function 'void A<T>::func(void)'
      with
      [
          T=B
      ]
      visual studio 2010\projects\temp\temp\main.cpp(13) : see reference to class template instantiation 'A<T>' being compiled
      with
      [
          T=B
      ]
  • 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-18T06:27:01+00:00Added an answer on May 18, 2026 at 6:27 am

    I’m not sure I understand what you’re asking, but it appears you are missing the essential CRTP cast:

    template<class T>
    struct A {
      void func() {
        T& self = *static_cast<T*>(this);  // CRTP cast
        self.func();
      }
    };
    
    struct V : A<V> {  // B for the case of virtual func
      virtual void func() {
        std::cout << "V::func\n";
      }
    };
    
    struct NV : A<NV> {  // B for the case of non-virtual func
      void func() {
        std::cout << "NV::func\n";
      }
    };
    

    If T does not declare its own func, this will be infinite recursion as self.func will find A<T>::func. This is true even if a derived class of T (e.g. DV below) declares its own func but T does not.

    Test with different final overrider to show dispatch works as advertised:

    struct DV : V {
      virtual void func() {
        std::cout << "DV::func\n";
      }
    };
    struct DNV : NV {
      void func() {
        std::cout << "DNV::func\n";
      }
    };
    
    template<class B>
    void call(A<B>& a) {
      a.func();  // always calls A<T>::func
    }
    
    int main() {
      DV dv;
      call(dv);   // uses virtual dispatch, finds DV::func
      DNV dnv;
      call(dnv);  // no virtual dispatch, finds NV::func
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: #include <iostream> using namespace std; class FooA { public: virtual
I have something like this: #include <iostream> #include <map> int main() { std::map<int, int*>
I have something like this: #include <iostream> namespace N { typedef std::pair<int, double> MyPair;
Supposing to have something like this: #include <map> int main(){ std::map<int,int> m; m[1] =
I have something like this in my Spring Application: public class Book{ public Book(){
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
I have something like this: <div class=content> <a href=#>A</a> </div> <div class=content> <a href=#>B</a>
I have something like this set up: class CategoryPage (webapp.RequestHandler): def get(self): ** DO
I have something like this: [Description(Sets the color.), Category(Values), DefaultValue(Color.White), Browsable(true)] public Color MyColor
I want to write a file_handle class like this #include <stdio.h> #include <iostream> #include

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.