重载operator()进行重构是否是一种好习惯? [英] Is overloading operator() for a reconstruction a good practice?

查看:106
本文介绍了重载operator()进行重构是否是一种好习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想以下情况:

class A {
    private:

    std::string id;
    std::array<std::string, 128> data;

    public:

    A(const std::string& id) : id(id) {}
    A(const A& other) : id(other.id), data(other.data) {}
    virtual ~A(){}

    //to override the intern data
    A& operator=(const A& other) {

        this->data = other.data;

        return *this;
    }

    //to override the whole element
    A& operator()(const A& other) {

        this->id = other.id;
        this->data = other.data;

        return *this;
    }
};

如您所见,我的想法是使用operator =覆盖内部数据,并使用operator()覆盖整个元素.我受到构造函数的启发,该构造函数允许A a(anOtherA);构造元素,并且我想重写此元素以进行重构.现在我不知道这是否可以智能地重载它,因为它实际上是函数调用运算符.

As you can see, my idea was to use operator= to override the internal data and operator() to override the whole element. I was inspired by the constructor which would allow A a(anOtherA); to construct the element and I would like to override this for a re-construction. Now I don't now if this would be smart overloading this because it's actually the function call operator.

推荐答案

重载operator()进行重构是否是一种好习惯?

总之,这不是良好实践.这样只会掩盖内幕.

In short no, that isn't good practice. Such just obfuscates what is done under the hood.

data提供设置器,并将您在重载的operator()中提供的代码用于分配operator=()的实现,将会提供更清晰自然的语义:

Providing a setter for data and use the code you provided in your overloaded operator() for the implementation of the assignment operator=() would provide the clearer and naturally expected semantics:

class A {
    private:

    std::string id;
    std::array<std::string, 128> data;

    public:

    A(const std::string& id) : id(id) {}
    A(const A& other) : id(other.id), data(other.data) {}
    ~A(){}

    //to override the intern data
    A& operator=(const A& other) {
        id = other.id;
        data = other.data;

        return *this;
    }

    //to override the intern data
    void setData(const A& other) {
         data = other.data;
    }
    void setData(const std::array<std::string, 128>& data_) {
         data = data_;
    }
};


operator()的语义没有明确定义(与operator=()相比),您可以像"normal" 函数调用那样调用类.最适用于将您的类型作为参数的模板.
但是我希望它更多地执行一些 action 而不是更改类的内部状态.


The semantics of the operator() isn't that clearly defined (vs the operator=()) beyond you can make a call of your class looking like a "normal" function call (which is mostly useful with templates taking your type as a parameter).
But I'd expect it more to do some action instead of changing the internal state of the class.

关于样式,而不是getter/setter函数的set/get前缀,我更喜欢c ++标准库中的操作(例如,使用

Regarding the style, instead of the set / get prefixes for getter/setter functions I prefer what's done in the c++ standard library, (like e.g. with the std::ios_base::flags() property):

class A {
private:
    // ...
    std::array<std::string, 128> data_;
public:
    const std::array<std::string, 128>& data() const {
         return data_;
    }
    void data(const std::array<std::string, 128>& data) {
         data_ = data;

    }
    // ...
};

这篇关于重载operator()进行重构是否是一种好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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