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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:15:26+00:00 2026-06-13T00:15:26+00:00

I am wondering if the following is possible. Lets say I have a code

  • 0

I am wondering if the following is possible.
Lets say I have a code like this:

template <class NumberType>
struct Number
{
   NumberType value;

   void operator = (Number in_val)
   {
        value = in_val;
   }
}

So then I would be able to do something like:

Number<int> n1, n2;
n2.value = 5;
n1 = n2;
cout << "Value: " << n1.value << endl;

But this won’t allow me to do the following:

Number<int> n1;
Number<double> n2;
n2.value = 5;
n1 = n2;
cout << "Value: " << n1.value << endl;

How to make this possible?
Do I have to wrap this struct/class with another OR do I have to make some fancy recursion?

p.s.
I have used C++ for some time now but never tried templates. So consider that I’m very new to templates.

— EDIT —
Ok I got it correctly now. But another related question came.

template<class OtherNumType>
Number& operator *= ( const OtherNumType& in_value)
{
    value *= in_value;
    return *this;
}

This gives a compilation error. Why? What is the correct way?

  • 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-13T00:15:27+00:00Added an answer on June 13, 2026 at 12:15 am

    When the template definition of Number<T> is considered by the compiler for any specific type T, the name Number (when used as a type name) is interpreted as Number<T>, whatever T may be at that point.

    Hence, for Number<int>, your current template definition provides only for the assignment operator below:

    void operator=(Number<int> in_val)
    

    because Number is interpreted as Number<int> at that point.

    In order to make the operator more flexible, you can turn it into a member template (a templated function in an already templated class):

    template <class NumberType>
    struct Number
    {
      NumberType value;
    
      template <typename T2>
      Number &operator=(const Number<T2> &in_val)
      {
        value = in_val.value;
        return *this;
      }
    };
    

    Note how I have modified the operator not only to accept Number<T2> for any type T2, but also make it return *this and accept the argument as const reference – that is the most common and useful way to define the assignment operator.

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

Sidebar

Related Questions

Lets say I have the following code: public class Base { // Some stuff
Possible Duplicate: generating random enums Lets say I have the following: enum Color {
I was wondering if the following is possible. Create a class that accepts an
I am wondering if the following is possible in MVC 3 I basically have
I'm wondering if the following is possible. I have a survey with 100+ questions,
I was wondering if the following was possible: class foo { virtual void bar();
I have the following in my web.config. This works fine but i'm wondering is
I'm using Hibernate 3.2.7.ga and I'm wondering if the following is possible. Let's say
Was wondering if the following is possible: Currently, I have a subset of nodes
I'm wondering if the following is possible: class A { public int SomeMethod() {

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.