cod*_*789 7 c++ scope typedef class member-functions
我有一个成员函数的类,它的返回类型很长:
/* MyClass.hpp */
namespace foo
{
class MyClass
{
public:
SomeNameSpace::Lengthy_return_type_name someFunction(params...);
};
}
Run Code Online (Sandbox Code Playgroud)
我想让它更具可读性,而不会让读者感到困惑,而且我不一定要将 typedef 暴露给外界。所以在 MyClass.hpp 中,我在 MyClass 中添加了以下 typedef:
/* MyClass.hpp */
namespace foo
{
class MyClass
{
private:
typedef SomeNameSpace::Lengthy_return_type_name myType;
public:
myType someFunction(params...);
};
}
Run Code Online (Sandbox Code Playgroud)
现在,在 MyClass.cpp 中,我陷入了困境,因为 myType 不可见,无法用作返回类型。如果我将 typedef 公开,我可以使用 MyClass::myType,但我不喜欢那样,因为我认为它令人困惑并且还暴露了 myType。我也可以将 typedef 添加到 MyClass.cpp 的顶部,但这可能会偏离意义,因为读者必须做一些挖掘才能找出类型所指的内容。
我是不是想多了?在这种情况下,最佳实践是什么?