首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++标准语法成员声明

C++标准语法成员声明
EN

Stack Overflow用户
提问于 2014-05-23 16:22:39
回答 1查看 131关注 0票数 1

在C++规范的语法中,类的成员定义为:

代码语言:javascript
复制
member-declaration:
  decl-specifier-seq(optional) member-declarator-list(optional);
  function-definition ;(optional)
  ::(optional) nested-name-specifier template(optional) unqualified-id ;//what is this?
  using-declaration
  template-declaration
  ...

我能理解其中的四个。但是第三个定义了一个强制嵌套的名称说明符,后面跟着一个id。e.g

代码语言:javascript
复制
class {
  X::Y::z;
}

我不知道任何与此定义相匹配的C++语法。我错过了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-23 16:58:02

答案可以在class.access.dcl部分找到。简而言之,这样的声明称为“访问声明”,其目的是更改继承成员的访问级别。

例如:

代码语言:javascript
复制
class A
{
protected:
    int a;
    int a1;
};

class B
{
public:
    int b;
    int b1;
};

class C : public A, private B
{
public:
    A::a;
    B::b;
}

int f()
{
    C c;

    c.a1; // access error: A::a1 is protected
    c.b1; // access error: B is private base of A

    c.a; // accessible because A::a is 'published' by C
    c.b; // accessible because B::b is 'published' by C
}

这种声明被using所取代,但为了兼容性的目的而保留。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23834139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档