C++类访问说明符详细程度
一个“传统”C++ 类(只是一些随机声明)可能类似于以下内容:
class Foo
{
public:
Foo();
explicit Foo(const std::string&);
~Foo();
enum FooState
{
Idle, Busy, Unknown
};
FooState GetState() const;
bool GetBar() const;
void SetBaz(int);
private:
struct FooPartialImpl;
void HelperFunction1();
void HelperFunction2();
void HelperFunction3();
FooPartialImpl* m_impl; // smart ptr
FooState m_state;
bool m_bar;
int m_baz;
};
如果原始程序员没有整齐地组织他的“访问区域”,我总是发现这种类型的访问级别规范很难看并且难以遵循。
以 Java/C# 风格查看相同的代码片段,我们得到:
class Foo
{
public: Foo();
public: explicit Foo(const std::string&);
public: ~Foo();
public: enum FooState
{
Idle, Busy, Unknown
};
public: FooState GetState() const;
public: bool GetBar() const;
public: void SetBaz(int);
private: struct FooPartialImpl;
private: void HelperFunction1();
private: void HelperFunction2();
private: void HelperFunction3();
private: FooPartialImpl* m_impl; // smart ptr
private: FooState m_state;
private: bool m_bar;
private: int m_baz;
};
在我看来,这在标头中更容易阅读,因为访问说明符就在目标旁边,而不是一堆行之外。我发现在使用未分成通常的“*.hpp/*.inl”对的仅标头模板代码时尤其如此。在这种情况下,函数实现的大小压倒了这个小但重要的信息。
我的问题很简单,源于我从未见过其他人在他们的 C++ 代码中主动这样做的事实。
假设我没有支持“类视图”的 IDE,那么使用这种冗长程度是否有任何明显的缺点?
欢迎任何其他风格建议!
A "traditional" C++ class (just some random declarations) might resemble the following:
class Foo
{
public:
Foo();
explicit Foo(const std::string&);
~Foo();
enum FooState
{
Idle, Busy, Unknown
};
FooState GetState() const;
bool GetBar() const;
void SetBaz(int);
private:
struct FooPartialImpl;
void HelperFunction1();
void HelperFunction2();
void HelperFunction3();
FooPartialImpl* m_impl; // smart ptr
FooState m_state;
bool m_bar;
int m_baz;
};
I always found this type of access level specification ugly and difficult to follow if the original programmer didn't organize his "access regions" neatly.
Taking a look at the same snippet in a Java/C# style, we get:
class Foo
{
public: Foo();
public: explicit Foo(const std::string&);
public: ~Foo();
public: enum FooState
{
Idle, Busy, Unknown
};
public: FooState GetState() const;
public: bool GetBar() const;
public: void SetBaz(int);
private: struct FooPartialImpl;
private: void HelperFunction1();
private: void HelperFunction2();
private: void HelperFunction3();
private: FooPartialImpl* m_impl; // smart ptr
private: FooState m_state;
private: bool m_bar;
private: int m_baz;
};
In my opinion, this is much easier to read in a header because the access specifier is right next to the target, and not a bunch of lines away. I found this especially true when working with header-only template code that wasn't separated into the usual "*.hpp/*.inl" pair. In that scenario, the size of the function implementations overpowered this small but important information.
My question is simple and stems from the fact that I've never seen anyone else actively do this in their C++ code.
Assuming that I don't have a "Class View" capable IDE, are there any obvious drawbacks to using this level of verbosity?
Any other style recommendations are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“入乡随俗。”
我花了很多时间使用 Java,喜欢为每个字段和方法单独指定访问说明符的风格。然而,当我用 C++ 编程时,我总是使用第一个代码片段中显示的样式。
"When in Rome, do as the Romans do."
I, having spent a lot of time with Java, like the style of specifying access specifiers for every field and method separately. However when I am programming in C++, I always use the style shown in your first code snippet.
就我个人而言,我发现必须为每个符号指定访问限定符非常烦人。它使事情变得更难阅读,而不是更容易,并且鼓励了在整个类定义中自由混合私有和公共内容的坏习惯。我经常看到这种混乱。在 C# 中,我尝试使用
#region private
等来缓解这种情况,这希望能鼓励未来的维护人员保持清洁。Personally, I find it very annoying to have to specify the access qualifier for every symbol. It makes things harder to read, not easier, and encourages the very bad habit of freely mixing private and public stuff throughout the class definition. I see this kind of mess all the time. In C#, I try to mitigate this with
#region private
, etc, which hopefully encourages future maintainers to keep things clean.这没有什么问题,尽管你会扬起眉毛。保持访问说明符分离的主要优点是它鼓励将所有私有成员和方法放在类的顶部 - 一起。
如果您的类太大而无法在整个屏幕上显示,那么它可能应该分解为多个类,和/或任何隐式内联函数都应该显式声明为内联,并将实现移出类。
There's nothing wrong with it, though you will raise eyebrows. The primary advantage of keeping the access specifiers separate is it encourages placing all the private members and methods at the top of the class -- together.
If your class is too big to fit on one screenfull than it probably should be either broken up into more than one class, and/or any implicitly inline functions should be explicitly declared inline with the implementation moved out of the class.
后一种方法的缺点是很少有人这样做,而且以这种方式让其他开发人员感到惊讶并不是一个好主意,而且它需要输入访问修饰符一百万次。使用传统方法将不必一遍又一遍地键入修饰符,这也是预期的方法。
The disadvantage to the latter approach is that is rarely done, and it is not a good idea to surprise other developers in this way, and it requires the access modifier to be typed a million times. Using the traditional approach will save having to needlessly type the modifier over and over and is also the expected approach.
事实上,从语义上讲,这没有什么区别,但如果您遵循已接受的内容,那么您将为您自己和您的同事带来很大帮助。
就我个人而言,我喜欢这样的课程:
但是,如果我提交到一个严格来说不属于我的存储库,我会毫不犹豫地改变我的方式,因为我知道如果有人按照我的要求将他们的代码提交到我的存储库,我会多么感激海关。
Indeed, semantically it makes no difference but you will do yourself and your colleagues a great favour if you'll just follow what's accepted.
Personally I like my class like so:
But I will change my manners without thinking twice if I commit into a repository that isn't strictly mine, because I know how appreciative I'd be if someone would commit their code to my repositories following my customs.