Jump to content

Talk:Dominance (C++)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Bad example

[edit]

The example is flawed and does not explicitly explain why Child1::function() is called. I found a more concise example on stack overflow which would change the example as follows:

class D
{
public:
    virtual void f();
};

class B : public virtual A
{
public:
    void f();
};

class C : public virtual A
{
};

class A : public B, public C
{
public:
    A()
    {
        f();
    }
};

The call to f() is resolved as follows. First, we add B::f and D::f to the set of names that could be considered. D::f doesn't hide anything because D has no base classes. However, B::f does hide D::f, so even though D::f can be reached from A without seeing B::f, it's considered hidden and removed from the set of objects that could be named f. Since only B::f remains, that's the one that's called. The ISO spec mentions (§10.2/7) that

But as I am not sure about copy/pasting explanations from somewhere else (even with a reference).. I'll just put it here and leave it up to the wikipedia experts .. hope that's ok with you. 79.193.222.207 (talk) 09:54, 26 August 2013 (UTC)[reply]

You're absolutely right; the old page was garbage. I've replaced it with a proper writeup and examples analogous to the one you got from StackOverflow. --Quuxplusone (talk) 06:00, 11 February 2014 (UTC)[reply]

Your example is nonsense and won't even compile. Assuming you meant B to inherit from D not A, B::f does NOT "hide" D:f. It overrides it. This has nothing to do with name lookup and everything to do with inheritence. — Preceding unsigned comment added by 74.67.66.223 (talk) 19:36, 19 February 2021 (UTC)[reply]

WRONG

[edit]

This article is completely WRONG. It explain the interacting between overloading and overriding. That is NOT dominance. Dominance has NOTHING to do with name lookup. Is has to do with multiple inheritance of the same virtual base class. If a class C inherits a base class A and a derived class B and the derived class B overrides one or more of A's method, C will inherit the method from B, not A. B's override has dominance. — Preceding unsigned comment added by 74.67.66.223 (talk) 19:30, 19 February 2021 (UTC)[reply]