简体   繁体   English

我需要本地扩展或外部方法的C ++?

[英]C++ where I'll need local extension or foreign method?

I'm not familiar with C++ enough to know where those 2 very similar refactorings (introduce local extension and introduce foreign method) will be needed. 我对C ++不够熟悉,无法知道在哪里需要这两种非常相似的重构(引入本地扩展并引入外部方法)。 I know cases that forces to use those, but I can't figure sample classes that will need this "trick". 我知道强迫使用这些案例的情况,但是我无法弄清楚需要这个“技巧”的示例类。 I'm making C++ refactoring tutorial, to help new developers, but it looks like I need yours help too :) 我正在制作C ++重构教程,以帮助新开发人员,但看来我也需要您的帮助:)

If you used it, please tell me in where and why. 如果您使用过它,请告诉我在哪里以及为什么。 Thanks in advance. 提前致谢。

"Foreign" methods are methods that operate on, but aren't members of a class. “外来”方法是可操作的方法,但不是类的成员。 In Java, that just means (static) methods of other classes that take the first class as an argument. 在Java中,这仅表示将第一个类作为参数的其他类的(静态)方法。 In C++, you'll need to be aware of free functions. 在C ++中,您需要了解自由函数。 Unlike Java , in C++ methods can exist outside classes. 与Java不同,C ++中的方法可以存在于类外部。 int main() is the most famous one. int main()是最著名的。 The STL is also packed with free functions. STL还带有免费功能。 Just have a peek in alone. 独自一人看看。

For Java, the site you linked advises that "foreign methods are a work-around" and suggest "Introduce Local Extension" as a alternative refactoring. 对于Java,您链接的站点建议“外部方法是一种解决方法”,并建议“引入本地扩展”作为替代重构。 In C++, free functions are not a work-around. 在C ++中,自由函数不是解决方法。 That means there's much less pressure to introduce local extensions. 这意味着引入本地扩展的压力要小得多。

Furthermore, in C++ functions are not virtual by default, and objects are often passed by value. 此外,在C ++中,函数默认情况下不是虚拟的,并且对象通常按值传递。 This means that the "introduce local extensions" technique often fails. 这意味着“引入本地扩展”技术通常会失败。

I hope this goes to show the fundamental problem with your approach: you're taking refactoring techniques for Java, which address specific Java weaknesses, and try to convert them into C++ which has a rather different set of weaknesses. 我希望这能显示出您的方法的基本问题:您正在采用Java的重构技术,以解决特定的Java弱点,并尝试将其转换为具有许多弱点的C ++。 Even where it works, it's often unnecessary. 即使在可行的地方,也通常是不必要的。

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

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