首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包装器类中赋值运算符的返回类型?

包装器类中赋值运算符的返回类型?
EN

Stack Overflow用户
提问于 2015-11-02 04:22:43
回答 1查看 116关注 0票数 2

我想出了以下代码来在C++中实现C#样式的属性。它基本上是一个围绕typename T的包装类,具有用户定义的get/set函数:

代码语言:javascript
复制
#include <functional>

template <typename T>
struct property
{
    property(std::function<T(void)> getter, std::function<void(const T&)> setter)
        : get(getter), set(setter) { }

    property(std::function<T(void)> _getter)
        : get(_getter), set([](const T&) { throw std::exception(); }) { }

    property<T>& operator=(const T& value) { set(value); return *this; } // w i h   o   s   h r ? 
    T operator=(const T& value) { set(value); return get(); }            //  h c   t   u e   e e   
    operator T() { return get(); }

    property<T>& operator=(property<T>& value) { set(value); return *this; };   //...
    T operator=(property<T>& value) { set(value); return get(); };              //...

    property<T>& operator=(property<T>&) = delete;

    // arithmetic / assignment
#define OP(__OP__) \
    T operator __OP__(const T& rhs) { return get() __OP__ rhs; } \
    T operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return get(); }              //...
    //property<T>& operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return *this; } //...
    OP(+); OP(-); OP(*); OP(/); OP(%); OP(^); OP(&); OP(|);
#undef OP

    // bit shift
#define OP(__OP__) \
    T operator __OP__(int rhs) { return get() __OP__ rhs; } \
    property<T>& operator __OP__##= (int rhs) { set(get() __OP__ rhs); return *this; }
    OP(<<); OP(>>);
#undef OP

    // comparison / logic
#define OP(__OP__) bool operator __OP__(const T& rhs) { return get() __OP__ rhs;}
    OP(<); OP(>); OP(==); OP(!=); OP(<=); OP(>=); OP(&&); OP(||);
#undef OP

    // inc/dec
#define OP(__OP__) \
    property<T>& operator __OP__##__OP__() { set(get() __OP__ 1); return *this; } \
    T operator __OP__##__OP__(int) { T value = get(); operator __OP__##__OP__(); return value; }
    OP(+); OP(-);
#undef OP

    T operator ~() { return ~get(); }
    bool operator !() { return !get(); }

private:
    std::function<T(void)> get;
    std::function<void(const T&)> set;
};

struct test
{ 
    property<int> a = property<int>([&]()
    {
        return x/10;
    }, 
    [&](int value) 
    {
        x = value*10;
    });

    property<int> b = property<int>([&]()
    {
        return y+10;
    }, 
    [&](int value) 
    {
        y = value-10;
    });

private:
    int x, y;
};

using namespace std;
void main()
{
    test x;

    x.a = 5;
    x.a = x.a + 15;
    x.a *= 5;
    x.b = x.a;
    x.b += x.a / 10;

    cout << "property a=" << dec << x.a << endl; // should be 100
    cout << "property b=" << dec << x.b << endl; // should be 110
}

这里我的主要问题是,赋值操作符应该返回T还是property<T>&?返回引用似乎更好,但我希望避免意外地重新分配整个类(包括get/set函数),尽管我相信重载operator=(property<T>&)和删除复制构造函数足以防止这一点。

无论如何,两者似乎产生了相同的结果,那么两者之间有什么不同呢?你更喜欢哪一个?为什么?

编辑,相关问题:在没有定义set函数的情况下,有没有什么方法可以在编译时检测赋值,而不是抛出运行时异常?

EN

回答 1

Stack Overflow用户

发布于 2015-11-02 09:41:04

在大多数情况下,通过副本返回可以正常工作,但也有一些不正常的情况。请考虑以下几点:

代码语言:javascript
复制
struct A { 
  A(int i) : i_{i} {} 
  int i_; 

  A operator=(A const& A) { i_ = a.i_; return *this; }
};

然后试试这个:

代码语言:javascript
复制
A a1{1};
A a2{2};
A a3{3};
(a3 = a2) = a1; // What is the value of a3::i_?

通过复制返回意味着a3 = a2赋值会生成一个重新赋值为等于a1的临时变量。换句话说,赋值之后的a3::i_ == 2。如果通过引用返回,则返回a3::i_ == 1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33466796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档