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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:29:45+00:00 2026-06-15T10:29:45+00:00

Is it possible to create a type (let’s say degrees ) and define specific

  • 0

Is it possible to create a type (let’s say degrees) and define specific operators for it? Such as: =, +, *, -, /, +=, *=, -=, /=.

I’m wondering this because I need to use degrees for one of my programs and I don’t want to use a float object because using degrees a; a.value = 120; a.value = b.value + a.value; is redundant over a simple degrees a = 120; a = b+a;.

Now why don’t I just use:

typedef float degrees;

? Well, because I need one more thing. When I write

degrees a;
a = 120;
a += 300;

a should be equal to 60 (420-360) because I don’t really need a = 6150 when I can have a = 30 with the same effect. So I’d overload those operators to keep the value between 0 and 360.

Is it possible? And, if so, how?

  • 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-15T10:29:46+00:00Added an answer on June 15, 2026 at 10:29 am

    The solution to your problem doesn’t need Boost or any other libraries. You can achieve what you want by using C++ classes, and overloading both the mathematical operators you want (+, -, *, /, etc) and the assignment operators you want (=, +=, -=, etc) and the comparison operators you want (<, >, <=, >=, etc)… or really any operators you want!

    For example:

    #include <cmath>
    
    class Degrees {
    public:
        // *** constructor/other methods here ***
        Degrees& operator=(float rhs) {
            value = rhs;
            normalize();
            return *this;
        }
        Degrees& operator+=(const Degrees &rhs) {
            value += rhs.value;
            normalize();
            return *this;
        }
        Degrees operator+(const Degrees &rhs) {
            return Degrees(value + rhs.value);
        }
    
    private:
        float value;
        void normalize() {
            value = std::fmod(value, 360);
        }
    };
    

    Then you can do things like this:

    Degrees a, b; // suppose constructor initializes value = 0 in all of them
    a = 10;
    b = 20;
    a += b; // now, a = 30.
    Degrees c = a + b; // now, c = 50.
    

    I’ve given you an example for overloading assignment and plus operators, but you can try this same thing with any other kind, and it should work.

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

Sidebar

Related Questions

Is it possible to create an object type inside of a package in Oracle
I'm using Umbraco 5. Is it possible to create new Document Type programmatically using
If i have a type x_typ and subtype y_typ, is it possible to create
Is it possible in xsd to create restriction based on elements of some type
I'm wondering if is possible pass types by argument in Java. Let me explain
I need a method that returns an instance of the supplied class type. Let's
Is it possible to use an enumeration in the following circumstance: Let’s say you
Let's consider this model: A: columns: b_id: type: int notnull: false relations: b: B
With Entity Framework and Code First, is it possible to let it create and
Let's say I have a C# type : class MyType<T> { T Value {

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.