开发者

C++ keyword using [duplicate]

This question already has answers here: Closed 11 years ago.

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&);
开发者_StackOverflow

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜