如何使用内置返回类型重载操作符? [英] How to overload operators with a built-in return type?

查看:131
本文介绍了如何使用内置返回类型重载操作符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个类,它包装一些数学运算。让我们使用玩具示例

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

float mFloat;
int mInt;
bool mIsFloat;
};

我想使用以下原型创建一个运算符重载:

  float operator =(const Test& test)
{
if(!test.mIsFloat) //在这种情况下实际上不做赋值
return test.mFloat; //在这种情况下做它。
}

所以我的问题是:我可以重载operator =类型?
如果是,是否有一种方法来引用内置类型?



我知道我可以这样做,如果我包装内置的一类。但在这种情况下,我想让赋值运算符在LHS上使用内置类型



使用示例:

 测试t(0.5f); 
float f = t; // f == 0.5
int i = 0;
i = t; //我保持0.



更新:非常感谢您的帮助。从玩具示例中扩展一点,让人们明白我真正想要做的。



我有一个配置系统,允许我从一个树获取配置参数



我可以从树中获取项目,操作如下:

  float updateTime = config [system.updateTime]; 

但是可能system.updateTime不存在。或者是错误的类型。一般为配置我有一个默认块,然后代码覆盖默认值从配置:

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

p>

  updateTime = config [system.updateTime]; 

如果存在覆盖,操作将成功。所以通常,如果从操作符[]的返回是树中的无效节点,那么赋值不会发生。



现在我用一个函数来解决它:

  getConfig(config,system.updateTime,updateTime); 

但我宁愿使用赋值运算符。



 如果我愿意创建类来封装内置函数,我可以这样做。class MyFloat 
{
operator =(const Test& test){if(test.isValidNode())f = test.float(); return * this; }
float f;
}

但是显然不优先使用简单的类包装内置函数重载分配。问题是 - 这是否可能在c ++?

解决方案

根据你的例子,你真正想要的是一个隐式转换操作符: / p>

  class Test 
{
// ...
public:
operator float()const;
};

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

如果您想有条件地完成分配,那么您需要采取另一种方法。一个命名的方法可能是最好的选择,所有的事情都考虑...这样:

  class Test 
{
public:
bool try_assign(float& f)const;
};

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

返回mIsFloat;
}

无论你做什么,注意你引入的语法糖不会导致在不可读代码中。


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++?

解决方案

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.

这篇关于如何使用内置返回类型重载操作符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆