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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:12:02+00:00 2026-05-16T21:12:02+00:00

According to Storing C++ template function definitions in a .CPP file it is easy

  • 0

According to
Storing C++ template function definitions in a .CPP file

it is easy to seprate the interface and the implementation of a template class,

.h file

template<typename T> 
class foo 
{ 
   public: 
     foo();
     ~foo();
     void do(const T& t); 
}; 

.cpp file

template <typename T> 
void foo::foo() 
{ 
    // initiate 
} 

//destructor

template <typename T> 
void foo::do(const T& t) 
{ 
    // Do something with t 
} 

void foo<int> fi; 
void foo<std::string> fs; 

the trick being to specialise the template class by creating instances at the end of the .cpp file.

But what if this was a pure virtual template class

.h file

template<typename T> 
class foo 
{ 
   public:
     foo();
     ~foo(); 
     virtual void do(const T& t) = 0; 
}; 

and we derive a concrete class from this:

template<typename T> 
class foobar : public foo<T>
{ 
   public: 
     void do(const T& t); 
}; 

the source file for this class looks like

.cpp file

template <typename T> 
void foobar::do(const T& t) 
{ 
    // Do something with t 
} 

void foobar<int> fi; 
void foobar<std::string> fs;

and the source file for foo looks the same except the initiations are removed (since ofcourse now foo is an abstract class).

But there’s a linker error now; foo<int>() is unresolved in the constructor of foobar<int>. This is fixed by moving the constructor and destructor of foo back to the header file from the source file.

So my question is how can we create abstract template base classes and keep the deifnition of the template class hidden in a source file??

  • 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-16T21:12:03+00:00Added an answer on May 16, 2026 at 9:12 pm

    You can explicitly instantiate a type without needing to instantiate a variable. Also, your existing code is hideously bugged and doesn’t even come close to compiling.

    template<typename T> 
    class foo 
    { 
       public: 
         foo();
         ~foo();
         void something(const T& t); 
    }; 
    
    template <typename T> 
    foo<T>::foo() 
    { 
        // initiate 
    } 
    
    //destructor
    
    template <typename T> 
    void foo<T>::something(const T& t) 
    { 
        // Do something with t 
    } 
    template<typename T>
    foo<T>::~foo() {
    }
    
    template class foo<int>;
    

    This will instantiate foo for int as a type but you don’t need to mess around with variables.

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

Sidebar

Related Questions

Let me present my problem with an example : template <typename T> class a{
I wrote a sample java file that is located in /src folder. public class
I'm writing a logging class that uses a templatized operator<< function. I'm specializing the
I'm renaming a file using boost::filesystem, and sometimes the target file will exist. According
I would like to make a class that has a function that saves the
Currently I am studying Standard Template Library (STL). In this program I am storing
Consider I have a some Factory class: class Factory { public: Factory(); virtual ~Factory();
Here is one of my header file which consists of a union template with
[question update according to updated requirements] I've implemented following function which should return either
I wrote an implementation of vector, which compiles but is not working properly according

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.