Dominance (C++)In the C++ programming language, dominance refers to a particular aspect of C++ name lookup in the presence of Inheritance. When the compiler computes the set of declarations to which a particular name might refer, declarations in very-ancestral classes which are "dominated" by declarations in less-ancestral classes are hidden for the purposes of name lookup. In other languages or contexts, the same principle may be referred to as "name masking" or "shadowing". The algorithm for computing name lookup is described in section 10.2 [class.member.lookup] of the C++11 Standard.[1] The Standard's description does not use the word "dominance", preferring to describe things in terms of declaration sets and hiding. However, the Index contains an entry for "dominance, virtual base class" referring to section 10.2. Example without diamond inheritancevoid f(double, double); // at global scope
struct Grandparent {
void f(int);
void f(double, double);
};
struct Parent : public Grandparent {
void f(int); // hides all overloads of Grandparent::f
};
struct Child : public Parent {
void g() { f(2.14, 3.17); } // resolves to Parent::f
};
In the above example, It is often surprising to new C++ programmers that the declaration of It is also important to observe that in C++, name lookup precedes overload resolution. If Example with diamond inheritancestruct Grandparent {
void f(int);
void f(double, double);
};
struct Mother : public Grandparent {
void f(int); // hides all overloads of Mother::Grandparent::f
};
struct Father : public Grandparent { };
struct Child : public Mother, Father { // Mother::Grandparent is not the same subobject as Father::Grandparent
void g() { f(2.14, 3.17); } // ambiguous between Mother::f and Father::Grandparent::f
};
In the above example, the compiler computes an overload set for Example with virtual inheritancestruct Grandparent {
void f(int);
void f(double, double);
};
struct Mother : public virtual Grandparent {
void f(int); // hides all overloads of Mother::Grandparent::f
};
struct Father : public virtual Grandparent { };
struct Child : public Mother, Father { // Mother::Grandparent is the same subobject as Father::Grandparent
void g() { f(2.14, 3.17); } // resolves to Mother::f
};
In this final example, the name
Even if See alsoReferences
Information related to Dominance (C++) |