<p>Given the following example, why do I have to explicitly use the statement <code>b-&gt;A::DoSomething()</code> rather than just <code>b-&gt;DoSomething()</code>?</p>

<p>Shouldn't the compiler's overload resolution figure out which method I'm talking about?</p>

<p>I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.)</p>

<pre><code>class A
{
  public:
    int DoSomething() {return 0;};
};

class B : public A
{
  public:
    int DoSomething(int x) {return 1;};
};

int main()
{
  B* b = new B();
  b-&gt;A::DoSomething();    //Why this?
  //b-&gt;DoSomething();    //Why not this? (Gives compiler error.)
  delete b;
  return 0;
}
</code></pre>

[{'answer_id': 72075, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 6, 'selected': True, 'text': "<p>The two “overloads” aren't in the same scope. By default, the compiler only considers the smallest possible name scope until it finds a name match. Argument matching is done <em>afterwards</em>. In your case this means that the compiler sees <code>B::DoSomething</code>. It then tries to match the argument list, which fails.</p>\n\n<p>One solution would be to pull down the overload from <code>A</code> into <code>B</code>'s scope:</p>\n\n<pre><code>class B : public A {\npublic:\n    using A::DoSomething;\n    // …\n}\n</code></pre>\n"}, {'answer_id': 72076, 'author': 'Pieter', 'author_id': 5822, 'author_profile': 'https://Stackoverflow.com/users/5822', 'pm_score': 4, 'selected': False, 'text': '<p>Overload resolution is one of the ugliest parts of C++</p>\n\n<p>Basically the compiler finds a name match "DoSomething(int)" in the scope of B, sees the parameters don\'t match, and stops with an error.</p>\n\n<p>It can be overcome by using the A::DoSomething in class B</p>\n\n<pre><code>class A  \n{  \n  public:  \n    int DoSomething() {return 0;}\n};  \n\nclass B : public A  \n{  \n  public:  \n    using A::DoSomething;\n    int DoSomething(int x) {return 1;} \n};   \n\n\nint main(int argc, char** argv)\n{\n  B* b = new B();  \n  // b-&gt;A::DoSomething();    // still works, but...\n  b-&gt;DoSomething();    // works now too\n  delete b;  \n  return 0;\n}\n</code></pre>\n'}, {'answer_id': 72081, 'author': 'Anthony Williams', 'author_id': 5597, 'author_profile': 'https://Stackoverflow.com/users/5597', 'pm_score': 2, 'selected': False, 'text': "<p>When you define a function in a derived class then it hides all the functions with that name in the base class. If the base class function is virtual and has a compatible signature then the derived class function also overrides the base class function. However, that doesn't affect the visibility.</p>\n\n<p>You can make the base class function visible with a using declaration:</p>\n\n<pre><code>class B : public A  \n{  \n  public:  \n    int DoSomething(int x) {return 1;};  \n    using A::DoSomething;\n};  \n</code></pre>\n"}, {'answer_id': 72082, 'author': 'Jono', 'author_id': 6552, 'author_profile': 'https://Stackoverflow.com/users/6552', 'pm_score': 1, 'selected': False, 'text': '<p>When searching up the inheritance tree for the function to use, C++ uses the name without arguments, once it has found any definition it stops, then examines the arguments. In the example given, it stops in class B. In order to be able to do what you are after, class B should be defined like this:</p>\n\n<pre><code>class B : public A  \n{  \n  public:\n    using A::DoSomething;\n    int DoSomething(int x) {return 1;};  \n}; \n</code></pre>\n'}, {'answer_id': 72086, 'author': 'slicedlime', 'author_id': 11230, 'author_profile': 'https://Stackoverflow.com/users/11230', 'pm_score': 1, 'selected': False, 'text': '<p>The function is hidden by the function with the same name in the subclass (but with a different signature). You can unhide it by using the using statement, as in using A::DoSomething();</p>\n'}, {'answer_id': 72142, 'author': 'Lehane', 'author_id': 142, 'author_profile': 'https://Stackoverflow.com/users/142', 'pm_score': 3, 'selected': False, 'text': '<p>No, this behaviour is present to ensure that you don\'t get caught out inheriting from distant base classes by mistake.</p>\n\n<p>To get around it, you need to tell the compiler which method you want to call by placing a using A::DoSomething in the B class.</p>\n\n<p>See <a href="http://becomeaprogrammerin21years.blogspot.com/2008/03/c-inheritance-oddity.html" rel="noreferrer">this article</a> for a quick and easy overview of this behaviour.</p>\n'}, {'answer_id': 72172, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>This has something to do with the way name resolution works. Basically, we first find the scope from which the name comes, and then we collect all overloads for that name in that scope. However, the scope in your case is class B, and in class B, B::DoSomething <strong>hides</strong> A::DOSomething:</p>\n\n<p>3.3.7  Name hiding                                [basic.scope.hiding]</p>\n\n<p>...[snip]...</p>\n\n<p>3 In a member function definition, the declaration of a local name hides\n  the  declaration  of  a  member  of  the class with the same name; see\n  <em>basic.scope.class</em>.  The declaration of a member in a  derived  class\n  (<em>class.derived</em>) hides the declaration of a member of a base class of\n  the same name; see <em>class.member.lookup</em>.</p>\n\n<p>Because of name hiding, A::DoSomething is not even considered for overload resolution</p>\n'}, {'answer_id': 72535, 'author': 'ugasoft', 'author_id': 10120, 'author_profile': 'https://Stackoverflow.com/users/10120', 'pm_score': 2, 'selected': False, 'text': "<p>That's not overloading! That's HIDING!</p>\n"}, {'answer_id': 74749, 'author': 'James Curran', 'author_id': 12725, 'author_profile': 'https://Stackoverflow.com/users/12725', 'pm_score': 3, 'selected': False, 'text': "<p>The presence of a method in a derived class hides all methods with the same name (regardless of parameters) in base classes.   This is done to avoid problems like this:</p>\n\n<pre><code>class A {} ;\nclass B :public A\n{\n    void DoSomething(long) {...}\n}\n\nB b;\nb.DoSomething(1);     // calls B::DoSomething((long)1));\n</code></pre>\n\n<p>than later someone changes class A:</p>\n\n<pre><code>class A\n{\n    void DoSomething(int ) {...}\n}\n</code></pre>\n\n<p>now suddenly:</p>\n\n<pre><code>B b;\nb.DoSomething(1);     // calls A::DoSomething(1);\n</code></pre>\n\n<p>In other words, if it didn't work like this, a unrelated change in a class you don't control (A), could silently affect how your code works.</p>\n"}]
['https://Stackoverflow.com/questions/72010', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12083/']