5

我正在查看一些开源代码,发现了这样的类声明:

class Foo{
    private:
        // declarations
    private:
        // declarations
    private:
        // declarations
    public:
        // declarations
};

有没有什么时候你想做这样的事情,除了在有长的声明清单时提醒你成员的隐私?

4

4 回答 4

4

这对于这种类型的场景特别有用:

class SomeClass
{
   // COnstructors etc.
   public:
      SomeClass();
      SomeClass(const SomeClass& other);
      ~SomeClass();
      SomeClass operator=(const SomeClass& other);
      SomeClass(const OtherClass& other); 

   // Internal use functions. 
   private:
       int SomePrivateFunc();
       int OtherPrivateFunc();

   // Functions that "do stuff" with this class. 
   public:
       int SomeFunc();
       int OtherFunc();

   // Private member variables. 
   private:
       int x, y; 

   // Public member variables.
   public:
       int value; 
}

(类似的评论// Constructurs etc.只是为了表明这是“这些东西属于一起”的一部分)

于 2013-07-18T12:42:35.233 回答
1

是的,您可以这样做以记住成员的隐私,还可以将您的类的数据类型、属性和方法等分开......

于 2013-07-18T12:30:23.043 回答
1

它没有错,你可能是对的,它可能是一个提醒,语义上与只使用一次相同。在我看来(和使用)不止一次使用一个部分会混淆和误导读者,并不是说更强烈的提醒是使用评论,特别是为了将成员组织成组。

于 2013-07-18T12:38:48.767 回答
0

我猜您在谈论 C++ 代码,在 C# 中,您假设在每个变量前面声明访问器,如 (public)。我猜它使代码更具可读性

于 2013-07-18T12:32:30.037 回答