1

In the grammar of C++ Specification, the members of a class is defined as this:

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

I understand 4 of them. But the 3rd one defines a mandatory nested name specifier followed by an id. e.g

class {
  X::Y::z;
}

I don't aware of any C++ syntax that matches this definition. Did I miss something?

JavaMan
  • 4,954
  • 4
  • 41
  • 69
  • No, that should belong to the first line: decl-specifier-seq + member-declarator-list is the way to define a member variable. – JavaMan May 23 '14 at 16:27
  • Also, nested-name-specifier matches something TERMINATED by :: e.g. X:: so it cannot match X::Y z. It only matches X::Y:: while z is the unqalified id – JavaMan May 23 '14 at 16:29
  • 1
    I'm tempted to -1 because this is dependent on the *version* of the C++ Standard - or, in other words, *this grammar rule does not appear in **the** C++ Standard, which is currently C++11*. You're referring to some earlier draft or former Standard, and this piece of information is missing in the question. – dyp May 23 '14 at 17:01

1 Answers1

4

The answer is to be found in the [class.access.dcl] section. To put shortly, such a declaration is called 'access declaration' and its purpose is to change access level for an inherited member.

For example:

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
}

This sort of declaration was superseded by using, but kept for compatibility purposes.

ach
  • 2,314
  • 1
  • 13
  • 23