10

I have an odd case with Visual Studio 2003. For somewhat legitimate reasons, I have the following hierarchy:

class A {};

class B : public A {
public:
    class A {};
};

class C : public B::A {};

That is, I have an inner class with the same name as a parent of the outer class. When C tries to inherit from B::A, Visual Studio thinks I'm pointing to the parent class A, not the nested class within B. GCC seems to resolve to the inner class version as I expected

Is this a Visual Studio 2003 bug, or am I doing it wrong? Is there a workaround (other than upgrading Visual Studio)?

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175

3 Answers3

5

This looks like a bug in Visual C++ 2003. Using Visual C++ 2012, B::A correctly names the nested class A, not the base class A.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • As for a workaround, I don't know: the design is rather questionable. I would rename the nested class. If I had Visual C++ 2003 installed I'd try to see what's up, but I haven't used Visual C++ 2003 in a few years. – James McNellis Aug 17 '12 at 16:39
  • I was stupid and didn't test my simplified example; turns out it was wrong. The updated one reproduces the problem – Michael Mrozek Aug 17 '12 at 17:04
2

Yes, this looks like VS2003 bug. Workaround is simple - use typedef, it works this way:

class A { public: int x; };
class B : public A { public: class A { public: int y; }; }; 

typedef B::A BA;

class C: public BA {};

void f()
{
   C cc;
   cc.y = 0;
}
Rost
  • 8,779
  • 28
  • 50
0

It looks like a VS bug,
I didn't know Thanks for posting.
I think The workaround will be a SafeInherit Template I don't know what will be a better name.

template <typename T>
struct SafeInherit{
  typedef T Type;
};

class B : public SafeInherit<A>::Type {
  public:
  class A {};
}
Neel Basu
  • 12,638
  • 12
  • 82
  • 146