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

The Archive Base Latest Questions

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

I want to overload the assignment operator for types like int, long etc. That

  • 0

I want to overload the assignment operator for types like “int”, “long” etc. That is, I want to use code like:

class CX {
private:
  int data;
...
};

CX obj;
int k;
k = obj;  // k should get the value of obj.data

Apparently assignment operators cannot be friend functions. How do I achieve the above?

I maybe missing something simple but just cant figure out the syntax/method to do this.

Also, one IMP restriction – we CANNOT use get/set methods because ::
In release code, we will have CX typedefed as int or long (as required), but in DEBUG code, we want to have it as a class (for automatic type checking in thousands of places). The code needs to be common.
Reason is that the compiler (atleast the version we are using) is somehow not able to optimize all the operations if CX is made a class.


One issue is – I dont want this to pass:

CX x; long p; p = x;

I assume the casting solution below will implicitly make the code for long/short etc pass too.
(If not, then it is exactly what I am looking for!).

On a related note, answering David’s question – the reason I want to refactor is – we want the ability to switch CX to be 32 bit or 64 bit. As such, we want to disallow any implicit conversions and catch them at compile time. Now, the reverse – (disallowiong

CX x = some_64_bit_int;

but allowing

CX x = some_32_bit_int;

I achieved by using a templatized = operator that asserts on compile time by default, but overloading it for my desired type.


In-case you feel this is a bad design or that I should try other alternatives – The reason why I need is this: We have thousands of lines of legacy code, where something is just typedefed to “int”.

typedef int CX;

There are assignments all over the place like:

CX x; int k; k = x;    // Writing the simplified relevant code here

I am working on a project to change CX to a class. In the first phase, we want to fix all compilation errors (that come on making CX a class) making as little changes to code as possible.

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

    You cannot do this. operator=() has to be a member function and cannot be overloaded for int. I see these possibilities:

    • Rely on an implicit conversion operator in your class. I would advice against that. I cannot remember a single time I have done this where I have not regretted and removed it later.
    • Write an explicit member function int get() const {return data;} and call this.
    • Ask yourself why you want to wrap that int in a class, but still want to allow assignment to plain int. That smells.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to overload the = operator on a simple C++ class called
I want to overload the conversion of an object to a string, so that
I have an enum, that looks like this: enum Suit {Clubs, Diamonds, Hearts, Spades};
I have a Person javascript class like function Person(_name, _id, _salary){ this.Name = _name;
Can you overload functions in PowerShell? I want to my function to accept a
Update: gutted the question with a simpler example, that isn't answered by the originally
I'm using private inheritance in the implementation of two very related classes. The using
I want to pass a diffrent number of strings to a function in java,
I have a simple query where I want to get the attributes distinct using

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.