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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:12:37+00:00 2026-05-27T03:12:37+00:00

#ifndef NUMBER_HPP #define NUMBER_HPP template <class T> class Number { public: Number( T value

  • 0
#ifndef NUMBER_HPP
#define NUMBER_HPP

template <class T>
class Number
{
public:
  Number( T value ) : m_value( value )
  {
  }

  T value() const
  {
    return m_value;
  }

  void setValue( T value )
  {
    m_value = value;
  }

  Number<T>& operator=( T value )
  {
    m_value = value;
  }

  //  template <class T2>
  //  Number<T2>& operator=( const Number<T>& number )
  //  {
  //    m_value = number.value();

  //    return *this;
  //  }

private:
  T m_value;
};

typedef Number<int> Integer;
typedef Number<float> Float;
typedef Number<double> Double;

#endif // NUMBER_HPP

The commented assignment operator overloading is my attempt to do what I want, I thought it might provide a better description than the one above the snippet.

I want to be able to do the following:

Float a(10);
Integer b(20);

a = b;

Where a then would be casted to an int and given the value of b, but still be an instance of class Number.

Is it possible? Can you help me out here?

Thanks in advance.

  • 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-27T03:12:38+00:00Added an answer on May 27, 2026 at 3:12 am

    You should do this:

    template <class T2>
    Number<T>& operator=( const Number<T2>& number )
    {
        m_value = number.value();
        return *this;
    }
    

    That is, use T2 in the parameter type, not in the return type!

    I would rather use different letter for template parameter:

    template <class U>
    Number<T>& operator=( const Number<U>& number )
    {
        m_value = number.m_value; //I would also directly access the member variable!
        return *this;
    }
    

    I think, it is better to use explicit cast, if you want to use class type as template argument and whose constructor has been declared explicit:

     m_value = static_cast<T>(number.m_value); 
    

    By the way, the other operator= should be implemented as:

    Number<T>& operator=(T const & value ) //accept by const reference
    {
        m_value = value;
        return *this; //you've to return!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#ifndef MACROS_NULLCHECK_H_ #define MACROS_NULLCHECK_H_ #include <assert.h> #define NULLCHECK(x) assert(x != (void *) 0); #endif
FILE #1 (foo.h): #ifndef FOO_H_ #define FOO_H_ #include baseclass.h #include bar.h class Bar; class
I have created two files: tunables.h #ifndef TUNABLES_H #define TUNABLES_H void tunables_load_conservative(); void tunables_load_aggressive();
I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;
If I create a class like so: // B.h #ifndef _B_H_ #define _B_H_ class
#ifndef DELETE #define DELETE(var) delete var, var = NULL #endif using namespace std; class
Here is the prototype of my class Rational #ifndef RATIONAL_H #define RATIONAL_H //forward declaration
I have the following preprocessor divective: #ifndef NDEBUG #define TRACE printf #else #define TRACE(...)
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
If I have the following: /* * example.h */ #ifndef EXAMPLE #define EXAMPLE #include

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.