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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:52:31+00:00 2026-06-01T16:52:31+00:00

I have a class whose only data member is a 32 bit unsigned integer.

  • 0

I have a class whose only data member is a 32 bit unsigned integer. That is, the class looks like this:

class A {
protected:
    unsigned int bits;
    /* ... some methods ... */
public:
    /* ... some methods ... */
};

But I want it to also be able to implicitly convert to and from 32 bit unsigned integers. So I added in a copy constructor as well as a casting operator:

class A {
protected:
    unsigned int bits;
    /* ... some methods ... */
public:
    A(): bits(0) {} // default constructor
    A(const unsigned int i): bits(i) {} // convert from unsigned int to A
    operator unsigned int() { return bits; } // convert from A to unsigned int
    /* ... some methods ... */
};

So now for example, I can do the following things:

int main () {
    // default constructor
    A a0 = A();
    // convert from unsigned int
    A a1 = 1 + 2;
    // conversions between the two types:
    unsigned int result = (a0 & a1 + 5) / 2;
}

However I’m struggling to get it to work with const types. In particular, the following doesn’t work:

int main () {
    const A a = 5;      // works
    unsigned int i = a; // compiletime error
}

It says “No suitable conversion function from ‘const A’ to ‘unsigned int’ exists.”. I’m using Visual Studio 2010 with the default compiler as provided by microsoft.

What conversion function do I need to create for it to work properly?

  • 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-01T16:52:32+00:00Added an answer on June 1, 2026 at 4:52 pm

    Declare the conversion operator const:

    operator unsigned int() const { return bits; }
    

    Caveat: Implicit conversion operators are almost always a bad idea. The dangers usually far outweigh the minor inconvenience of calling a plain old member function:

    unsigned int asInt() const { return bits; }
    ⋮
    unsigned int i = a.asInt();
    

    The ultimate solution, if you have C++11, is to declare an explicit conversion operator:

    explicit operator unsigned int() const { return bits; }
    ⋮
    unsigned int i = static_cast<int>(a);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that serves as a data model. I'll simplify it like
We have a class whose semantic behaviour is like the following :- struct Sample
I have a class whose constructor takes a const reference to a string. This
I have a class whose functionality I'd like to depend on a set of
I have a class whose instances have attributes that are containers which themselves contain
I have a CurrentUser class with a static property whose value was stored in
Suppose I have a file X.h which defines a class X, whose methods are
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class that represents users. Users are divided into two groups with different
I am implementing a templated sparse_vector class. It's like a vector, but it only

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.