[英]Class member function declared outside of public, protected and private
I came across some code that looked like this:我遇到了一些看起来像这样的代码:
class SomeClass {
void SomeFunction();
public:
~~ public members
}
There were no private or protected members of the class. class 没有私有或受保护成员。
My question is;我的问题是; is not labelling what part of the class the 'SomeFunction' belongs to bad programming?
没有标记 class 的哪个部分“SomeFunction”属于不良编程? Or does defining the function in this way implicitly assign it to some part of the class, ie private or protected, since it is not part of the public members?
或者以这种方式定义 function 是否隐含地将其分配给 class 的某些部分,即私有或受保护,因为它不是公共成员的一部分?
In C++, the difference between class
and struct
is that for a struct
everything is implicitly public
and for a class
everything is implicitly private
.在 C++ 中,
class
和struct
之间的区别在于,对于struct
,所有内容都是隐式public
的,而对于class
,所有内容都是隐式private
的。
As soon as you use explicitly public:
, private:
or protected:
the behavior is the same in both cases for the following members.只要您明确使用
public:
、 private:
或protected:
在两种情况下,以下成员的行为都是相同的。
This is true about inheritance too. inheritance 也是如此。
struct A: B
is a public inheritance. struct A: B
是公共 inheritance。
class A: B
is a private inheritance. class A: B
是私有 inheritance。
In your example, SomeFunction()
is in the implicit part of the class
, so it is considered private
.在您的示例中,
SomeFunction()
位于class
的隐式部分,因此它被视为private
。
A very well respected C++ expert considers this kind of declaration (implicitly private
) as a good practice.一位非常受人尊敬的 C++ 专家认为这种声明(隐式
private
)是一种很好的做法。
( https://howardhinnant.github.io/classdecl.html ) ( https://howardhinnant.github.io/classdecl.html )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.