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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:15:38+00:00 2026-05-12T18:15:38+00:00

I have a decorator-like pattern with a base that requires a constructor parameter. The

  • 0

I have a decorator-like pattern with a base that requires a constructor parameter. The decorator is constructed such that it can take an arbitrary number of add-on components as template parameters (up to 3 in this example).

Unfortunately, I can’t figure out how to pass the base’s constructor parameter to it when more than one add-on is specified. In the example below, CMyClass< AddOn_A > A( 100 ); works perfectly, but CMyClass< AddOn_A, AddOn_B > AB( 100 ); generates an error at the CMyClass constructor.

template< class Base >
class AddOn_A : public Base
{
public: 
    AddOn_A( int x ) : Base( x )
    {
    };

    int AddOne()
    {
        return static_cast< Base* >( this )->DoSomething() + 1;
    };
};

template< class Base >
class AddOn_B : public Base
{
public: 
    AddOn_B( int x ) : Base( x )
    {
    };

    int AddTwo()
    {
        return static_cast< Base* >( this )->DoSomething() + 2;
    };
};

class CBase
{
public:
    explicit CBase( int x ) : x_( x )
    {
    };

    int DoSomething()
    {
        return x_;
    };

private:
    int x_;
};

// define an empty AddOn
template< class > class empty {};

// forward declaration and Add-On defaults
template< template< class > class AddOn1 = empty,
          template< class > class AddOn2 = empty,
          template< class > class AddOn3 = empty >
class CMyClass;

// specialized template for the default case
template<> class CMyClass< empty, empty, empty > {};

// actual definition
template< template< class > class AddOn1,
          template< class > class AddOn2,
          template< class > class AddOn3 >
class CMyClass : public AddOn1< CBase >,
                 public CMyClass< AddOn2, AddOn3 >
{
public:
    // what needs to go here???
    CMyClass( int x ) : AddOn1< CBase >( x )
    {};
};

int _tmain( int argc, _TCHAR* argv[] )
{
    // works
    CMyClass< AddOn_A > A( 100 );
    _ASSERT( A.AddOne() == 101 );

    // works
    CMyClass< AddOn_B > B( 100 );
    _ASSERT( B.AddTwo() == 102 );

    // generates an error at the CMyClass ctor:
    // error C2512: 'CMyClass<AddOn1>' : no appropriate default constructor available
    CMyClass< AddOn_A, AddOn_B > AB( 100 );
    _ASSERT( AB.AddOne() == 101 );
    _ASSERT( AB.AddTwo() == 102 );

    return 0;
}

If anybody can point out what I may be doing wrong, please let me know.

Thanks,
PaulH

  • 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-12T18:15:39+00:00Added an answer on May 12, 2026 at 6:15 pm

    Your errors are generally originating out of the fact that CMyClass does not have a default constructor (because you define a CMyClass(int) instead), so it is necessary to explicitly instantiate your parents with the CMyClass(int) constructor that you have. So, for example, in your definition of CMyClass you need to add the call to CMyClass(int) in the initializer list

    CMyClass(int x) : AddOn1<CBase>(x), CMyClass<AddOn2, AddOn3>(x) //send x down
    

    Now that we have CMyClass sending x down the line, it is necessary for your base case specialization (CMyClass<empty, empty, empty>) to now have a constructor that accepts x but does nothing with it

    template<>
    class CMyClass<empty, empty, empty> {
    public:
        CMyClass(int) {} //do nothing
    };
    

    Now the compiler can find the right constructors and create your classes as you expect


    Just to explain why lines like CMyClass<AddOn_A> A(100) work, it’s because A (in that example) has only one parent, CMyClass<empty, empty, empty>, and your specialization

    template<> class CMyClass< empty, empty, empty > {};
    

    does have a default constructor, because it’s empty (or, more formally, because it defines no other constructors). This breaks down immediately once you call CMyClass<AddOn_A, AddOn_B> AB(100) because that has two parents, CMyClass<AddOn_B, empty, empty> and CMyClass<empty, empty, empty>, however the former does not have a default constructor, so the compiler does not know how to construct it. That’s why we must add that one line to the initializer list, so we tell the compiler to create CMyClass<AddOn_B, empty, empty> using its CMyClass(int x) constructor (note how that means the compiler will also try to make CMyClass<empty, empty, empty> with the x parameter, so we need to add a constructor to that specialization which will accept the parameter).

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

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Set the Order property. [CustAttribute1(Order=2)] [CustAttribute2(Order=1)] public ActionResult Index() {… May 12, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer This is the default property for controls and can be… May 12, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer If you're looking to "integrate Django and Orbited", you might… May 12, 2026 at 10:52 pm

Related Questions

I have a class that I've been provided that I really don't want to
I'm currently working on a legacy system using Oracle's ADF Faces JSF implementation for
I am currently designing a public-facing C++ API for a product which will require
I'm attempting to integrate SiteMesh into a legacy application using Tomcat 5 as my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.