开发者

Visual Studio 2010 debugger doesn't show values when using multiple inheritance

开发者 https://www.devze.com 2023-01-30 20:44 出处:网络
In my application I have a class for which I need to keep a set of interfaces delivered by the application.

In my application I have a class for which I need to keep a set of interfaces delivered by the application.

Since the class itself doesn't know all the possible interfaces, it can't simply keep one data member for every interface. Instead, it should keep a vector of interfaces.

In practice this means that I define one 'generic' interface (IGeneric) of which all the more specific interfaces inherit. That way, I can easily keep a vector with all the interfaces (in the code hereafter I replaced the actual interface names by some dummy names; in my application the names are much more meaningful):

#include <vector>
#include <windows.h>

class IGeneric
   {
   public:
      virtual ~IGeneric() {}
   };

class IFirst : public IGeneric {public: virtual void one() = 0;};
class ISecond : public IGeneric {public: virtual void two() = 0;};
class IThird : public IGeneric {public: virtual void three() = 0;};
class IFourth : public IGeneric {public: virtual void four() = 0;};

typedef std::vector<IGeneric *> GenericContainer;

To install the interfaces, other modules (which really know all the interfaces), provide functions to install the interfaces. They could be something like this:

void installFirst (GenericContainer &bc, IFirst &b) { bc.push_back(&b); }
void installSecond (GenericContainer &bc, ISecond &b) { bc.push_back(&b); }
void installThird (GenericContainer &bc, IThird &b) { bc.push_back(&b); }
void installFourth (GenericContainer &bc, IFourth &b) { bc.push_back(&b); }

In practive, applications will write classes that implement multiple of these interfaces, like this:

class DoAll : public IFirst, public ISecond, public IThird, public IFourth
   {
   public:
      virtual void one() {}
      virtual void two() {}
      virtual void three() {}
      virtual voi开发者_开发百科d four() {}
      DoAll(int i) : m_i(i) {}
   private:
      int   m_i;

};

Installing this implementation is now really easy. This is the main routine from my example:

int main() { GenericContainer gc;

DoAll all (123);

installFirst(gc,all); installSecond(gc,all); installThird(gc,all); installFourth(gc,all);

DebugBreak(); }

At the breakpoint, I now investigate the contents of the vector in the debugger. It looks like this:

Visual Studio 2010 debugger doesn't show values when using multiple inheritance

As you can see, it shows that the implementation of the interfaces is DoAll (in all 4 cases), and it even shows as which interface (IFirst, ISecond, ...) DoAll has been installed in the vector.

Problem is that the debugger doesn't seem to be able to show the contents of the most derived class (DoAll). It show the datamember m_i, but it is unable to show the value of m_i.

Only if I explicitly take the first interface address, and cast it to "DoAll *", I can correctly see the contents:

Visual Studio 2010 debugger doesn't show values when using multiple inheritance

I compile and link using the following commands:

cl /c /EHsc /GR /Zi /Od test.cpp
link /debug test.obj

Is this a bug in Visual Studio 2010 or am I missing something?

EDIT:

If I use single inheritance, like in this class:

class DoFirst : public IFirst
   {
   public:
      virtual void one() {}
      DoFirst(int i) : m_i(i) {}
   private:
      int m_i;
   };

And install it in the vector like this:

DoFirst f (456);
installFirst (gc,f);

The debugger shows me the correct values and it also immediately shows the most derived class:

Visual Studio 2010 debugger doesn't show values when using multiple inheritance


In vector you are holding pointers to IGeneric. IGeneric does not have any clue about m_i, which is member of DoAll. We might even say that there actually is a bug, because the debugger is showing {m_i = ???}, but it should not.

0

精彩评论

暂无评论...
验证码 换一张
取 消