Quick Q: In C++ are static member functions inherited? If yes why ambiguity error does not arise?

Quick A: Yes, and there are no ambiguity with static members.

Recently on SO:

In C++ are static member functions inherited? If yes why ambiguity error does not arise?

It's fine according to the lookup rules. You see, when you write member access (obj.display();), the member display is looked up not just in the scope of the class and its base classes. Base class sub-objects are taken into consideration as well.

If the member being looked up is not static, since base class sub-objects are part of the consideration, and you have two sub-objects of the same type, there's an ambiguity in the lookup.

But when they are static, there is no ambiguity. And to make it perfectly clear, the C++ standard even has a (non-normative) example when it describes class member lookup (in the section [class.member.lookup])

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.