是否可以在没有预处理器宏的情况下为getter-setter定义“虚拟”默认实现

时间:2021-03-04 07:11:49

It is possible to use template for default implemetation of getter-setter.

可以使用模板进行getter-setter的默认实现。

For instance - http://www.kirit.com/C%2B%2B%20killed%20the%20get%20%26%20set%20accessors/A%20simple%20meta-accessor. Most important, that if you decide to override default behaviour of such setter or getter, you can easly do this without need to change "client" code, because setter-getter calling syntax is same to calling methods, i.e.:

例如 - http://www.kirit.com/C%2B%2B%20killed%20the%20get%20%26%20set%20accessors/A%20simple%20meta-accessor。最重要的是,如果您决定覆盖此类setter或getter的默认行为,您可以轻松地执行此操作而无需更改“client”代码,因为setter-getter调用语法与调用方法相同,即:

an_object.an_int( 3 );
int i = an_object.an_int();

In both cases an_int can be object with operator() or method of an_object. After overriding re-compilation will be required in "client" code.

在这两种情况下,an_int可以是带有operator()的对象或an_object的方法。重写后,“客户端”代码中将需要重新编译。

But is it possible to define "virtual" default implemetation for getter-setter without preprocessor macros? i.e. important thing here is that during overide recompilation of "client" code is not needed. Of course it is possible to do with preprocessor, by I wonder, is there any more elegant solution?

但是没有预处理器宏可以为getter-setter定义“虚拟”默认实现吗?即重要的是,在重复编译“客户端”代码时不需要。当然,有可能与预处理器一起做,我想知道,还有更优雅的解决方案吗?

For my knowladge of C++03 is not possible, but maybe someone has some ideas, or maybe it is possible in C++11?

因为我对C ++ 03的了解是不可能的,但也许某人有一些想法,或者可能在C ++ 11中有可能?


Answer for "David Rodríguez - dribeas": something like this:

回答“DavidRodríguez - dribeas”:的内容如下:

#define accessor(type,name) \
virtual type name() {return m_##name;} \
type m_##name;

It can be overrided in derived class without need of recompilation of "client" code.

它可以在派生类中重写,而无需重新编译“客户端”代码。

2 个解决方案

#1


0  

As long as the functions aren't inline, you won't need to recompile the client code if you reimplement the functions. You will just need to relink the client code with the new implementation.

只要函数不是内联函数,如果重新实现这些函数,就不需要重新编译客户端代码。您只需要使用新实现重新链接客户端代码。

#2


0  

Not in a generally useful way. It's possible that it might almost work for very specific use-cases. The burden of maintenance to support these one offs is rarely worth the effort though.

不是一般有用的方式。它可能几乎适用于非常具体的用例。支持这些一次性的维护负担很少值得努力。

To do this you push a lot of complexity down into this "field" type which you're writing. This complexity will not generalize well. It will be a huge mess that isn't any easier to use than just writing the accessors yourself.

要做到这一点,你要将很多复杂性推到你正在编写的这个“字段”类型中。这种复杂性不会很好地概括。这将是一个巨大的混乱,并不比仅自己编写访问器更容易使用。

If writing these accessors is time consuming, write free helper functions to make it easier. These free functions are concrete and direct because they only do one thing. Collecting behavior in this way is much more flexible and maintainable over time, even if it is perceived to be a bit more verbose.

如果编写这些访问器非常耗时,请编写免费的辅助函数以使其更容易。这些免费功能具体而直接,因为它们只做一件事。以这种方式收集行为随着时间的推移更加灵活和可维护,即使它被认为更加冗长。

This also has much better encapsulation. Really, you're dumping a ton of implementation details into your interface that isn't interesting and shouldn't be there.

这也有更好的封装。实际上,你正在向你的界面倾倒大量的实现细节,这些细节并不重要,不应该存在。

#1


0  

As long as the functions aren't inline, you won't need to recompile the client code if you reimplement the functions. You will just need to relink the client code with the new implementation.

只要函数不是内联函数,如果重新实现这些函数,就不需要重新编译客户端代码。您只需要使用新实现重新链接客户端代码。

#2


0  

Not in a generally useful way. It's possible that it might almost work for very specific use-cases. The burden of maintenance to support these one offs is rarely worth the effort though.

不是一般有用的方式。它可能几乎适用于非常具体的用例。支持这些一次性的维护负担很少值得努力。

To do this you push a lot of complexity down into this "field" type which you're writing. This complexity will not generalize well. It will be a huge mess that isn't any easier to use than just writing the accessors yourself.

要做到这一点,你要将很多复杂性推到你正在编写的这个“字段”类型中。这种复杂性不会很好地概括。这将是一个巨大的混乱,并不比仅自己编写访问器更容易使用。

If writing these accessors is time consuming, write free helper functions to make it easier. These free functions are concrete and direct because they only do one thing. Collecting behavior in this way is much more flexible and maintainable over time, even if it is perceived to be a bit more verbose.

如果编写这些访问器非常耗时,请编写免费的辅助函数以使其更容易。这些免费功能具体而直接,因为它们只做一件事。以这种方式收集行为随着时间的推移更加灵活和可维护,即使它被认为更加冗长。

This also has much better encapsulation. Really, you're dumping a ton of implementation details into your interface that isn't interesting and shouldn't be there.

这也有更好的封装。实际上,你正在向你的界面倾倒大量的实现细节,这些细节并不重要,不应该存在。