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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:42:22+00:00 2026-06-14T12:42:22+00:00

In one of my projects I’m using the same CRTP approach (deriving from enable_crtp

  • 0

In one of my projects I’m using the same CRTP approach (deriving from enable_crtp) as in Answer 1 here: How do I pass template parameters to a CRTP?

However I have the need to derive from the derived classes too. Is there any way to make this work with out falling back to just static_cast the this pointer but by using the self() method from the Enable CRTP base class?

#include "EnableCRTP.h"

template<typename DERIVED>
class BASE : public EnableCRTP<DERIVED>
{
    friend DERIVED;
public:
    void startChain()
    {
        self()->chain();
    }
};

template<typename DERIVED>
class Derived1 : public BASE<Derived1<DERIVED> >
{
public:
    void chain()
    {
        std::cout << "Derived1" << std::endl;

        //self()->chain2(); <- compile Error
        static_cast<DERIVED*>(this)->chain2(); // <-Works
    }
};

class Derived2 : public Derived1<Derived2>
{
public:
    void chain2()
    {
        std::cout << "Derived2" << std::endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Derived2 der;    
    der.startChain();    
    return 0;
}
  • 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-06-14T12:42:23+00:00Added an answer on June 14, 2026 at 12:42 pm

    You can give the most-derived class as the template parameter to the CRTP base class, so that it will have access to all its members. Instead of

    template<typename DERIVED>
    class Derived1 : public BASE<Derived1<DERIVED> >
    

    Use:

    template<typename DERIVED>
    class Derived1 : public BASE<DERIVED>
    

    There are also other issues with your code. For example, you can’t directly call self() like you do because the compiler doesn’t know that self is a member of the base class (which is dependent on a template parameter). Instead, call this->self(). See this FAQ entry.

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

Sidebar

Related Questions

In one of my projects, I'm using an n-tier architecture DAL (Repository Pattern) <->
For one of my projects, I'm using the DOMDocument class to load and manipulate
Here's my problem: I have 2 projects - one 'common' projects with acts like
I am using FullCalendar in one my projects and would like to plot recurring
I've got a feature enablement mechanism in a couple of projects (one using RSpec
After numerous projects one-way asynchronous message passing looks like the only way to write
I have two projects.One project is build in MVC asp.net and the other project
I have a solution with 3 projects.One of UI (contains web pages) and one
I have the digdog MapKitDragAndDrop in one my projects to achieve drag and drop
I've got a solution with many projects One of these proejcts is a MyProject.Web

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.