c++name-clashname-conflictname-collision

Name conflict between member function and class name


I'm writing a chess program with wxWidgets. At one point, I have a subclass of wxGLCanvas, and it makes use of a Move class I wrote. Unfortunately, it seems like there's a method wxWindowBase::Move(), and so all my statements of the form list<Move> li won't compile.

Is there any nice way to resolve this? I've tried list< ::Move> li, and that does fix the problem, but it's gross, and I have to make that change everywhere. Unlike with namespace conflicts, a using-declaration doesn't seem to help here.


Solution

  • There are a few ways to disambiguate a base class hidden by a method name.

    typedef Move::Move Move_Base; // 1. the LHS of :: operator ignores functions
    using typename Move::Move; // 2. non-template "typename" avoids constructor
    typedef class Move Move_Base; // 3. elaborated type specifier
    typedef ::Move Move_Base; // 4. namespace qualification (as mentioned)
    

    (1) may not work in GCC due to a bug. (Not sure; you can try and see.)

    (2) is perhaps the most elegant, but the compiler has to get some C++11 nuances right. The typename keyword there is illegal in C++03, but I think it's not necessary. In C++11, the syntax refers to the constructor instead of the type name unless you say typename.

    You'll need to qualify the inherited function as wxWindowBase::Move().

    (3) only works when the base class is part of the current namespace, which often it shouldn't be.

    (4) is a bit of a pain because a class hard-coded against its enclosing namespace needs modification if moved to another namespace. As you mentioned, a bit ugly.