简体   繁体   English

标签c ++ class setter getter宏vs模板

[英]c++ class property setter getter Macro vs template

Im trying to add setter and getter functions to my classes. 我正在尝试向我的班级添加setter和getter函数。

In my mind it makes my classes look neater doing it like this. 在我看来,这样做会使我的班级看起来更加整洁。

I wanted to know what way is better, using a macro or using a template, what is faster? 我想知道哪种方法更好,使用宏还是使用模板,哪种方法更快?

#define PropertyM(type, var) \
private: \
    type _##var; \
public: \
    type Get##var() { return _##var; }\
    void Set##var(type val) { _##var = val; }


template<typename T>
class Property
{
protected:
    T m_val;

public:
    inline Property() {}
    inline Property(T val) { m_val = val; }

    inline void Set(const T &a) { m_val = a; }
    inline T Get() { return m_val; }

    operator T() { return m_val; }
    T &operator=(const T &a) { return m_val = a; }
};


class Test
{
public:
    Test();
    ~Test();

    Property<float> TemplateFloat;
    PropertyM(float, MacroFloat)
};

Test::Test() : TemplateFloat(0.f), _MacroFloat(0.f)
{
    // Just a example
    if (TemplateFloat != 1.f)
        TemplateFloat = 1.f;

    if (TemplateFloat.Get() != 2.f)
        TemplateFloat.Set(2.f);

    if (GetMacroFloat() != 1.f)
        SetMacroFloat(1.f);
}

Test::~Test()
{
}

From the point of view a client function, 从客户端功能的角度来看,

Property<float> TemplateFloat;

is no better then 那再好不过了

float myFloat;

You are exposing a member variable and letting the user work with a member variable directly. 您将公开一个成员变量,并让用户直接使用成员变量。

The macro provides not only a private member variable but also a public getter function and a public setter function. 该宏不仅提供private成员变量,还提供public getter函数和public setter函数。

Based on that, I think the macro method is better. 基于此,我认为宏方法更好。

As written, this is public data. 如所写,这是公共数据。 The methods you use will be optimized away in your binary, so it won't even give you ABI stability between versions. 您使用的方法将在二进制文件中进行优化,因此它甚至不会为您提供版本之间的ABI稳定性。

The dress you put it in (a template or a macro) is just lipstick on a pig. 您放入的衣服(模板或宏)只是口红在猪身上。

If you want zero-cost abstraction around a member variable, then there is also zero benefit. 如果要在成员变量周围进行零成本抽象,那么收益也为零。

If you want a layer of indirection that gives you ABI stability, none of these do it. 如果您需要一个间接层来提高ABI的稳定性,那么这些都不是。

If you want the ability to change the implementation of these templates/macros for special cases, then what you want to change it to needs to be factored in. Even if only a set of samples of what you might want to do. 如果您希望能够在特殊情况下更改这些模板/宏的实现,则需要考虑要将其更改为的内容。即使您可能想要做的只是一组示例。


As a general rule, if you have a solution that involves using macros and another that doesn't, don't use macros. 通常,如果您有一个涉及使用宏的解决方案,而另一个不使用宏的解决方案,则不要使用宏。 Macros do not respect scope or namespaces, most compilers are horrible at debugging through them, and while template errors are extremely long macros have the same kind of errors but don't provide you with the diagnostics to help you find out the problem. 宏不遵守作用域或名称空间,大多数编译器都无法通过它们进行调试,并且模板错误非常长,宏具有相同的错误类型,但是没有为您提供诊断信息以帮助您发现问题。

As an aside, in both cases, you are lacking proper consideration for rvalue semantics. 顺便说一句,在两种情况下,您都缺少对右值语义的适当考虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM