1

Possible Duplicate:
Why shall I use the “using” keyword to access my base class method?

Hi,

I can't find out why I needed the keyword using in the following case:

// Pure virtual class.
class Visitor
{
  public:
      void operator()(Ast&);

      virtual void operator()(Node&) = 0;
};

// Define the default visit methods.
class DefaultVisitor : public Visitor
{
  public:
    using Visitor::operator();      // First 'using' needed.

    virtual void operator()(Node&);
};

// A visitor using DefaultVisitor's behaviour.
class AVisitor : public DefaultVisitor
{
  public:
    using Visitor::operator();      // Second 'using' needed.

    virtual void operator()(Node&);
};

Without the two using statements, the public non-virtual method declared and defined in Visitor, void operator()(Ast&);, is not visible when called from AVisitor. For example:

AVisitor v;
Ast* ast = new Node(); // Node is-a Ast
v(*ast); // should call Visitor::operator()(Ast&);

will not compile, saying the method void operator()(Ast&) does not match anything in AVisitor. The only solution is to use using keyword to import the abstract methods of the base class. But why ? Since it is public, I don't understand why this is needed.

Thank you.

Community
  • 1
  • 1
Julio Guerra
  • 5,523
  • 9
  • 51
  • 75

1 Answers1

1

See this Why should I use the "using" keyword to access my base class method?

Also this

using

The using keyword is used to import a namespace (or parts of a namespace) into the current scope. Example code: For example, the following code imports the entire std namespace into the current scope so that items within that namespace can be used without a preceeding “std::”.

using namespace std;

Alternatively, the next code snippet just imports a single element of the std namespace into the current namespace:

using std::cout;

Related Topics: namespace

Using is for namespace specifications/use - not as I think you are trying to use it.

Community
  • 1
  • 1
Tim
  • 20,184
  • 24
  • 117
  • 214
  • 3
    Better leave that as a comment and close as duplicate, imho. – Xeo Jun 03 '11 at 15:52
  • Ok. Pretty simple. I always thought the compiler was using the arguments on every available methods. Thank you and sorry for the duplicate, using is so common that it is very hard to find something really related to the keyword. – Julio Guerra Jun 03 '11 at 15:58