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

The Archive Base Latest Questions

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

I want to create a calculator template < typename T > class Calculator {

  • 0

I want to create a calculator

template < typename T >
class Calculator
{
    public :
    Calculator ( void );
    ~Calculator ( void );

    T add(T a, T b)
    {
       return ( a + b ) ;
    }
};

Now I want to make this Caculator add strings, so add(“Tim”,”Joe”) shall give me “TimJoe” .

Can I use template function specialization for achieving this by making necessary changes to the existing class .

  • 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:02:28+00:00Added an answer on May 18, 2026 at 6:02 am

    Why would you want to ?

    Calculator<std::string> a;
    std::cout << a.add("Hello", " world") << std::endl;
    

    This code outputs “Hello world”. It’s not optimal (std::string parameters in the add member function are taken by value), but it does not require a specialization to work.

    EDIT OK, you want specialization, but you can’t just specialize the add member function because the function is not template itself. You can either specialize the whole class :

    template<>
    class Calculator<std::string>
    {
    public :
        Calculator() {}
        ~Calculator() {}
    
        std::string add(const std::string &a, const std::string &b)
        {
            // Do std::string specific stuff
            return a + b ;
        }
    };
    

    Or make the add member function template and specialize it :

    class Calculator
    {
    public :
        Calculator () {}
        ~Calculator () {}
    
        template<class T>
        T add(T a, T b)
        {
           return a + b;
        }
    };
    
    template<>
    std::string Calculator::add<std::string>(std::string a, std::string b)
    {
        // Do std::string specific stuff
        return a + b ;
    }
    

    With the second solution, a single Calculator instance will then be able to add int, std::string or whatever you need (as it’s the add function which is template, not the Calculator class itself).

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

Sidebar

Related Questions

i want create image animation , i have 50 images with png format now
I want to create 5 variables of type array all at once. Is this
I have to create app which has requirement to integrate calculator. I want to
I have created a nice scientific calculator and now want to save inputs and
I am want to make a calculator on visual studio 2010 using MFC application.
I need to create a Mortgage Calculator for my Java class, and I have
I want to create an application that uses a shipping calculator on an external
I want to create calculator which have 2 edit text name input1 and input2
I want to open calculator which I created. This is the code I wrote:
I want to make a simple calculator My code is below: package som.dev.android.calc; import

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.