c++ - C++标准语法的成员声明

标签 c++ grammar c++03

在C++规范的语法中,一个类的成员是这样定义的:

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
  ...

我理解其中的 4 个。但是第三个定义了一个强制性的嵌套名称说明符,后跟一个 id。例如

class {
  X::Y::z;
}

我不知道有任何符合此定义的 C++ 语法。我错过了什么吗?

最佳答案

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

例如:

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 取代,但出于兼容性目的而保留。

关于c++ - C++标准语法的成员声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23834139/

相关文章:

c++ - 如何使用 C++ 迭代器调用 Win32 API 函数 `WriteFile()`?

c++ - 为什么我不能在 c++0x 的 lambda 中声明一个结构变量?

grammar - bool /条件表达式的 ANTLR v3 语法

语法 : How to add a level of precedence

c++ - 强制实例化派生类型而不是基类型

c++ - 指向其他 vector 元素的指针的 vector

c++ - concurrent_vector 在 parallel_for ( PPL ) 中不起作用

grammar - 使用 Perl 6 语法在原始正则表达式中传递变量

c++ - 在 MSVC++ 中覆盖内存分配器

c++ - 为什么 std::setprecision(6) 在固定宽度模式下流式传输超过六位数字?