Topic: using-directives and dynamic binding


Author: dcrocker@eschertech.ccoomm ("David Crocker")
Date: Thu, 1 Apr 2004 18:14:54 +0000 (UTC)
Raw View
I have a question about using-directives. Consider the following:

class A {
public:
  virtual int f() const { return 0;}
}

class B : public A {
public:
  int f(int x) const { return x; }   // hides the inherited f()
  using A :: f;    // un-hides the inherited f()
  int g() const { return f(); }
}

class C : public B {
public:
  int f() const { return 1; }
}

int foo() {
 B* temp = new C;
 return temp->g();
}

The declaration of g() in class B relies on the directive "using A :: f;" to
make the declaration of f() inherited from A visible. This is described in
section 10.2 para 2 of ISO/IEC 14882:1998(E). But will the call to f()
within g() bind statically to A :: f() (as it would if I explicitly said
"return A :: f()" instead of using the using-directive); or dynamically to A
:: f() or C :: f() depending on the type of the 'this' pointer at run-time?

In other words, should the call "foo()" return 0 or 1?

I don't find the Standard clear on this point. Section 10.2 ("Member name
lookup") is silent on whether a call to a declaration that has been made
visible by a using-directive is statically or dynamically bound. Section
10.3 ("Virtual functions") is silent on whether a using-directive affects
whether binding is static or dynamic in the way that explicit scope
resolution does.

--
David Crocker
Escher Technologies
www.eschertech.com



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]