![](/img/trans.png)
[英]Why can't this public member function call decltype on a private struct member declared inside the class?
[英]Class member function declared outside of public, protected and private
我遇到了一些看起来像这样的代码:
class SomeClass {
void SomeFunction();
public:
~~ public members
}
class 没有私有或受保护成员。
我的问题是; 没有标记 class 的哪个部分“SomeFunction”属于不良编程? 或者以这种方式定义 function 是否隐含地将其分配给 class 的某些部分,即私有或受保护,因为它不是公共成员的一部分?
在 C++ 中, class
和struct
之间的区别在于,对于struct
,所有内容都是隐式public
的,而对于class
,所有内容都是隐式private
的。
只要您明确使用public:
、 private:
或protected:
在两种情况下,以下成员的行为都是相同的。
inheritance 也是如此。
struct A: B
是公共 inheritance。
class A: B
是私有 inheritance。
在您的示例中, SomeFunction()
位于class
的隐式部分,因此它被视为private
。
一位非常受人尊敬的 C++ 专家认为这种声明(隐式private
)是一种很好的做法。
( https://howardhinnant.github.io/classdecl.html )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.