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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:10:04+00:00 2026-06-13T17:10:04+00:00

Say I have a class, that wraps some mathematic operation. Lets use a toy

  • 0

Say I have a class, that wraps some mathematic operation. Lets use a toy example

class Test
{
public:
   Test( float f ) : mFloat( f ), mIsInt( false ) {}

   float mFloat;
   int   mInt;
   bool  mIsFloat;
};

I’d like to create an operator overload with the following prototype:

float operator=( const Test& test ) 
{ 
    if ( !test.mIsFloat ) return *this; // in this case don't actually do the assignment
    return test.mFloat;                      // in this case do it.
} 

So my questions are: can I overload operator= with a built-in return type?
and if so, is there a way to refer to the built-in type?

I know I could do this if I wrapped the built-ins with a class. But in this case I want to have the assignment operator work with built-in types on the LHS

Example of usage:

Test t( 0.5f );
float f = t; // f == 0.5
int   i = 0;
i = t;       // i stays 0.

UPDATE: Thanks so much for the help. Expanding a little bit from the toy example so people understand what I’m really trying to do.

I have a configuration system that allows me to get config parameters from a tree of parameters with different type ( they can be integers, floats, strings, arrays etc. ).

I can get items from the tree with operations like this:

float updateTime = config["system.updateTime"];

But it is possible that “system.updateTime” does not exist. Or is of the wrong type. Generally for configuration I have a block of defaults, and then code to overide the defaults from the config:

float updateTime = 10;
const char* logFile = "tmp.log";
... etc etc...

I want to do something like:

updateTime = config["system.updateTime"];

Where the operation succeeds if there is an override. So generally the assignment doesn’t happen if the return from operator[] is an “invalid” node in the tree.

Right now I solve it with a function like:

getConfig( config, "system.updateTime", updateTime );

But I would prefer to use assignment operator.

I could do this if I were willing to create classes to wrap the builtins.

class MyFloat
{
   operator=( const Test& test ) { if (test.isValidNode() ) f = test.float(); return *this; }
   float f;
}

But obviously it would be prefereable not to wrap built-ins with trivial classes just to overload assignment. Question is – is this possible in c++?

  • 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-13T17:10:05+00:00Added an answer on June 13, 2026 at 5:10 pm

    Based on your example, what you really want is an implicit conversion operator:

    class Test
    {
        // ...
    public:
        operator float() const;
    };
    
    inline Test::operator float() const
    {
        return mIsFloat ? mFloat : mInt;
    }
    

    If you want to conditionally do the assignment, then you need to take another approach. A named method would probably be the best option, all things considered… something like this:

    class Test
    {
    public:
        bool try_assign(float & f) const;
    };
    
    inline bool Test::try_assign(float & f) const
    {
        if (mIsFloat) {
            f = mFloat;
        }
    
        return mIsFloat;
    }
    

    Whatever you do, be careful that the syntactic sugar you introduce doesn’t result in unreadable code.

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

Sidebar

Related Questions

Say I have a class that wraps the Controller class: public class MyController :
Let's say I have a class that requires configuration, dependency injection etc. public class
Let me explain best with an example. Say you have node class that can
Lets say I have a class A that is fairly simple like this -
lets say i have a background worker in a class that perform db query
Let's say that I have class , that uses some functionality of dict .
Say I have a class that looks like this (get/set omited): class InfoClass{ String
Let's say we have a class that embed another parametric one, that has to
Say I have a class which represents a person, a variable within that class
Let's say I have a very simple PrototypeJS class that looks like this: var

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.