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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:59:34+00:00 2026-05-26T15:59:34+00:00

In designing a solution, sometimes it may be convenient to provide wrapper classes for

  • 0

In designing a solution, sometimes it may be convenient to provide wrapper classes for primitive data types. Consider a class that represents a numeric value, be it a double, a float, or an int.

class Number {
private:
    double val;

public:
    Number(int n) : val(n) { }
    Number(float n) : val(n) { }
    Number(double n) : val(n) { }

    // Assume copy constructors and assignment operators exist

    Number& add(const Number& other) {
        val += other.val;
        return *this;
    }

    int to_int() const { return (int) val; }
    float to_float() const { return (float) val; }
    double to_double() const { return val; }
};

Now suppose that I have a function as such:

void advanced_increment(Number& n) {
    n.add(1);
}

And I would use this function as such:

Number n(2);
advanced_increment(n); // n = 3

This sounds easy enough. But what if the function was like this?

void primitive_increment(int& n) {
    ++n;
}

Note that the increment is an example. It is assumed that the function would perform more complicated operations on primitive data types that they should also be able to perform on Number types without any issues.

How would I use the function exactly as before? As in:

Number n(2);
primitive_increment(n);

How could I make my Number class compatible with primitive_increment? How could I create a wrapper class for primitive data types that would be compatible anywhere that these data types are required?

So far, I have only found two solution. One is to create a function such as double& Number::get_value() and then use it like primitive_increment(n.get_value());. The second solution is to create implicit conversion methods such as Number::operator int&(); but these can result in many ambiguous calls and would make the code confusing.

I’m wondering if there is any other solution to implement these types of wrappers and retain their primitive functionality.

Update:

To further clarify, in the actual project, the intent here is to make all data types derived from one base class that is commonly referred to as Object when designing such solution. A constraint is that no outside library should be used. Therefore, if I have a container that has pointers to the type Object, it should be able to hold any arbitrary value, primitive or not, and perform any primitive operation that is allowed on Object. I hope this explains it better.

  • 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-26T15:59:35+00:00Added an answer on May 26, 2026 at 3:59 pm

    C++11 has explicit operator overloads.

    struct silly_wrapper {
      int foo;
      explicit operator int&() { return foo; }
    };
    
    void primitive_increment(int& x) { ++x; }
    
    
    int main()
    {
       silly_wrapper x;
       primitive_increment(x); // works
       x += 1; // doesn't work - can't implicitly cast
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked with designing an ecommerce solution. The aspect that is causing
When designing LINQ classes using the LINQ to SQL designer I've sometimes needed to
We are designing a solution that will be used on an internal network. One
I'm designing a REST solution for remote communication with a number of servers that
Problem: When designing the website I sometimes used width: xxx; on images. That way
I am designing a J2SE application, and looking for a solution for monitoring and
While designing a table my colleague here says that I should avoid identity column
When designing a collection class, is there any reason not to implement locking privately
Thanks for taking time to read this question. I am designing a 3-tier solution
I'm designing an application that'd require different themes. By 'theme' I mean the colors

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.