The using declaration and class members (C++ only)

A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A.

You would need a using declaration in a class definition if you want to create a set of overload a member functions from base and derived classes, or you want to change the access of a class member.

Read syntax diagramSkip visual syntax diagramusing declaration syntax
 
>>-using--+-+----------+--+----+--nested_name_specifier--unqualified_id--;-+-><
          | '-typename-'  '-::-'                                           |
          '-::--unqualified_id--;------------------------------------------'
 

A using declaration in a class A may name one of the following:

The following example demonstrates this:

struct Z {
  int g();
};

struct A {
  void f();
  enum E { e };
  union { int u; };
};

struct B : A {
  using A::f;
  using A::e;
  using A::u;
//  using Z::g;
};

The compiler would not allow the using declaration using Z::g because Z is not a base class of A.

A using declaration cannot name a template. For example, the compiler will not allow the following:

struct A {
  template<class T> void f(T);
};

struct B : A {
  using A::f<int>;
};

Every instance of the name mentioned in a using declaration must be accessible. The following example demonstrates this:

struct A {
private:
  void f(int);
public:
  int f();
protected:
  void g();
};

struct B : A {
//  using A::f;
  using A::g;
};

The compiler would not allow the using declaration using A::f because void A::f(int) is not accessible from B even though int A::f() is accessible.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]