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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:23:39+00:00 2026-05-24T00:23:39+00:00

In short: How do I define operator= for my class such that it compiles

  • 0

In short:
How do I define operator= for my class such that it compiles only if rhs is 0 (obj = 0)
but gives compilation error if rhs is non-0 value.
I know it is possible, forgot how.

Longer:
I have class C. I want to allow assignment obj = 0 for object of this class (which means, reset the object), but assignment from no other integer or pointer is defined. No conversion from integer or from pointer is defined other than obj=0.

C obj;
obj = 0; // reset object

Inside the operator=, I can do assert(rhs == 0), but that’s not good enough.
I know it is possible
to define operator= such that
it gives compilation error if rhs is not 0. Forgot details.
Can anybody refill ?

Thanks

  • 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-24T00:23:40+00:00Added an answer on May 24, 2026 at 12:23 am

    Use a pointer-to-member:

    class foo
    {
        // Give it a meaningful name so that the error message is nice
        struct rhs_must_be_zero {};
    
        // The default operator= will still exist. If you want to
        // disable it as well, make it private (and the copy constructor as well
        // while we're at it).
    
        foo(const foo&);
        void operator=(const foo&); 
    
    public:
        foo& operator=(int rhs_must_be_zero::*) { return *this; }
    };
    

    Since you can’t access foo::rhs_must_be_zero, you cannot name a pointer to member from this class. The only pointer to member you can name is the null pointer, aka the literal zero.

    Demo at http://ideone.com/bT02z

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

Sidebar

Related Questions

In short: is there some way I can modify a class definition such that
If I define a pointer to an object that defines the [] operator, is
Short question - how do you define your view models? Here are some of
There is a static class Pipe, defined in C++ header that I'm including. The
In F#, the equality operator (=) is generally extensional, rather than intensional. That's great!
I've got a generic range class and I'm trying to add a comparison operator
I'm working with an open-source library and they define a class like so: class
I am trying to build a C# class from a XSD file. In short
I'm currently developing a syntaxic analyser class that needs, at a point of the
I have some short defines in one of my headers like this: #define ROUND_DOWN(a,b)

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.