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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:30:19+00:00 2026-06-10T16:30:19+00:00

I have a question about template classes. For example, take this class template<class TBase>

  • 0

I have a question about template classes.
For example, take this class

template<class TBase> class CTemplateInherit : public TBase
{
public:

    virtual void DoNonSpecializedWork();
    virtual void DoTemplateWork();
    virtual ~CTemplateInherit();
};

// In header file
template<class TBase>
bool CTemplateInherit<TBase>::DoTemplateWork()
{
    std::wcout << L"CTemplateInherit::DoTemplateWork" << std::endl;
    TBase::DoWork();
    return true;
}

// In CPP file
bool CTemplateInherit::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

Now, I would like to be able to define non-specialized methods in a CPP file with dllexport, and just keep the specializations in the header.
Normally, I suppose I could just define member methods as templated, but since I’m inheriting from TBase that isn’t possible.

So how would I be able the split this up? I only override maybe 4 methods from TBase, and would like to be able to keep the other 40 or so methods as DLLEXPORT in the DLL, while the specializations are in the header file.

Thank you in advance for your suggestions!

  • 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-10T16:30:20+00:00Added an answer on June 10, 2026 at 4:30 pm

    I think there is one important piece of information missing: Are the non-specialized methods overriding virtual methods of the TBase class? This is the central issue here.

    If not, then you can just create another class for the non-specialized methods, and inherit (publicly) from both classes in your CTemplateInherit class. Problem solved.

    If, however, the non-specialized methods override virtual methods of the TBase class, then it is only slightly more complicated:

    Solution 1) Take all the non-specialized functions and regroup them into one “detail” header. Either as a group of (non-template) free-functions (if it is easy enough to just pass input-outputs as parameters) or as a (non-template) class with the non-specialized data members it requires. Then, you define all those functions (implement them) inside a corresponding cpp file (which you will later compile into the DLL).

    Then, in your CTemplateInherit class template, simply forward the non-specialized function calls to the set of “detail” functions. Because templates inline by default, the overhead will be nil. If you needed to regroup the “detail” functions into one class (non-template), then, simply use private inheritance, preventing function-name conflicts. Then, you can access the data members of the “detail” class like any other inherited data member, and you can forward the non-specialized function calls to the “detail” class implementation which you can compile into a DLL (if you have a suitable framework for exporting classes from a DLL (because plain C++ doesn’t have a reliable mechanism for that), but your question seems to imply that you do).

    The only issue with this solution is the annoying creation of several simple forwarding functions. But IMHO, that is a reasonable price to pay for tucking away an implementation in a DLL (almost any time you want to do that, you end up writing a bunch of wrapper functions).

    Solution 2) If there is a set of non-specialized virtual functions from the base class, then that subset is certainly not dependent on the actual type of TBase (I mean, the prototypes of those functions can be formed without it). Then, it means that you can regroup that subset of functions into another base-class from which TBase is expected to be inheriting from. Let’s call it TNonSpecialBase. At this point, you can set up the following hierarchy:

    class TNonSpecialBase {
      // set of pure-virtual functions that are not special.
    };
    
    // this is an example of a class that could act as a TBase:
    class TExampleBase : public virtual TNonSpecialBase {
      // set of virtual functions that are special to TExampleBase.
    };
    
    class CNonSpecialDerived : public virtual TNonSpecialBase {
      // declaration of non-specialized functions that override the function in TNonSpecialBase.
    };
    
    template <typename TBase>
    class CTemplateInherit : public TBase, public CNonSpecialDerived {
      // set of virtual functions that are specialized for the template argument TBase.
    };
    

    With the above setup, the specialized functions must end up in the header file of CTemplateInherit, but the non-specialized functions, regrouped in CNonSpecialDerived can be defined in a separate cpp file (and compiled into a DLL). The magic trick here is the use of the virtual inheritance to allow for the final class to have a single virtual-table for the base class TNonSpecialBase. In other words, this allows the class CNonSpecialDerived to override the virtual functions in TBase which are inherited from TNonSpecialBase, as long as TBase does not override any of those functions (in which case, the compiler will call it ambiguous). This way the user can deal with a pointer to a TBase object, call any of its virtual functions, which will cause either a dispatch towards the CTemplateInherit implementations (specialized) or the CNonSpecialDerived implementations (presumably, in the DLL).

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

Sidebar

Related Questions

I have a question about inheritance and template methods. Suppose I have this two
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I have a (simple) question about generic classes in c++. I have some knowledge
hi i have a question about the silverlight/xna template for wp7 programming. i start
I recently asked this question about how to simulate type classes in D and
I have a hierarchical class structure like this: Category -> Template -> Instance A
I have a question about template specialization in C++, and I am hoping someone
I have a question about the calendar class. I can populate days in the
I have a question related to a template class implementation design. A template AT
Let's say I have the following two classes: public class TestResults { public string

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.