重构:将友谊委派在子任务中
我重构一个代码,其中一堂课有一个朋友函数做很多事情。
class Foo
{
friend void do_something(Foo& foo);
};
void do_something(Foo& foo)
{
// More than 2000 lines of ugly code
}
我想在几个小功能中将do_something
的内容分开。看起来像这样的东西:
void do_something(Foo& foo)
{
if(case_1) do_1(foo);
else if(case_2) do_2(foo);
else if(case_3) do_3(foo);
//...
}
是否有一个设计,我可以将友谊转移到子形式do_x(foo&)
而不必在班级中声明它们,还是类似的东西以将代码分开?
注意: C ++ 11仅
注意:我不想将子函数写成MacCros
I refactor a code where a class has a friend function doing a lot of stuff.
class Foo
{
friend void do_something(Foo& foo);
};
void do_something(Foo& foo)
{
// More than 2000 lines of ugly code
}
I would like to split the content of do_something
in several small functions. Something looking like this :
void do_something(Foo& foo)
{
if(case_1) do_1(foo);
else if(case_2) do_2(foo);
else if(case_3) do_3(foo);
//...
}
Is there a design where I can transfert the friendship to the sub-fuctions do_x(Foo&)
without having to declare them in the class, or something similar in order to split the code ?
Note : C++11 only
Note : I don't want to write the sub-functions as maccros
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您创建一个类似的类
dosomehting
,而不是让do_something
作为函数呼叫其他子函数。然后,您可以将此类声明为
朋友类Dosomehting;
的朋友。因此,这些子函数可能是其私人方法。呼叫的方法 - 可以是一种名为void dosomehting :: process(foo& foo)
的公共方法:Instead of having
do_something
as function calling other sub-functions, I would suggest you to create an analogous classDoSomehting
.Then you could declare this class as a friend with
friend class DoSomehting;
. So these sub-functions could be its private methods. The method to call -- could be a public method named e.g. likevoid DoSomehting::process(Foo& foo)
: