0

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.

Henry Swanson
  • 186
  • 13

2 Answers2

3

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • No permutation of 2) seems to work, but somehow I forgot about typedefs, which will more-or-less fix my problem. Thank you! – Henry Swanson Dec 30 '14 at 07:34
  • @HenrySwanson Hmm, [GCC](http://coliru.stacked-crooked.com/a/306b81401a5b46bd) accepts the declaration, but Clang doesn't, which looks like a bug. However, GCC still [doesn't](http://coliru.stacked-crooked.com/a/3b2cf5115e59aeac) interpret such a declaration as resolving the ambiguity, which is another bug. – Potatoswatter Dec 30 '14 at 13:44
1

Put your code into a namespace. Then your class will be something::Move and it won't conflict as easily.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • That seems like the same problem though; it's just a difference of which namespace it's in. I like being able to have `using something::Move` in my .cpp files, so things like `std::list` become `list`, which is much more readable, IMO. – Henry Swanson Dec 30 '14 at 07:17
  • I don't understand: if your code is in `namespace something`, then you can say `list` without `using` it. – John Zwinck Dec 30 '14 at 07:20
  • I can, but it still mistakes `Move` the class for `Move` the method. – Henry Swanson Dec 30 '14 at 07:25
  • @HenrySwanson: maybe you should just rename your class and sidestep this mess. – John Zwinck Dec 30 '14 at 07:28
  • I could, but what else would I call a class that stores 'this is where the piece went and if it captured'? I was just hoping there was some way of making it pretty; I can live with the extra `::`. – Henry Swanson Dec 30 '14 at 07:30
  • `Action`? I dunno, we're bike shedding now. :) – John Zwinck Dec 30 '14 at 07:36