4

Suppose I have two versions of operator-> (overloaded on const) in a base class. If I say

using Base::operator->;

in a derived class, will I get access to both versions or just the non-const one?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662

3 Answers3

4

Same business as name hiding. It's all or nothing. Using declarations (7.3.3) bring a name, not a member.

ISO/IEC 14882 (2003), 7.3.3. 1/ A using-declaration introduces a name into the declarative region in which the using-declaration appears. That name is a synonym for the name of some entity declared elsewhere.

I encourage you to read 7.3.3, there are subtle things inside. You cannot using-declare a template, all the members refered by the name you using-declare must be accessible, the names are considerd for overload resolution alongside the names in the block the using declaration is found (ie. they don't hide anything), etc, etc.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
3

You get access to all versions of a method/operator with the same name in that parent.

Mark B
  • 95,107
  • 10
  • 109
  • 188
2

both. did you try it? (damn this answer is short: ah well, here is example:

#include <iostream>
#include <string>

struct bar
{
  void foo() { std::cout << "non_c:foo()" << std::endl; }
  void foo() const { std::cout << "c:foo()" << std::endl; }
};

class base
{
public:
  bar* operator->() { return &b; }
  bar const* operator->() const { return &b; }

private:
  bar b;  
};

class derived : public base
{
public:
  using base::operator->;
};


int main(void)
{
  const derived d = derived();
  derived e;

  d->foo();
  e->foo();
}
Nim
  • 33,299
  • 2
  • 62
  • 101
  • 3
    I refuse to learn language semantics by trying something with one particular compiler. – fredoverflow Aug 09 '11 at 14:00
  • @Fred, true - but pure curiosity? Else why not simply read the standard? – Nim Aug 09 '11 at 14:04
  • 2
    @Nim: reading the entire standard each time you want to know something is a bit impractical, as is memorizing the whole thing. SO works better as an index than the actual index ;-) – Steve Jessop Aug 09 '11 at 14:10
  • @Steve - my sentiments exactly, normally with these things, I try first, then confirm if it is the standard behaviour... but I guess each has their own approach... – Nim Aug 09 '11 at 14:15
  • 1
    @FredOverflow: And it is much better to ask a random crowd? I would go to the standard and check... – David Rodríguez - dribeas Aug 09 '11 at 17:02