[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: C++ help, please...
From:       "Tomas Mecir" <mecirt () gmail ! com>
Date:       2006-05-30 12:34:59
Message-ID: 492258b10605300534h679c9d30i25c7c1e1f316ec24 () mail ! gmail ! com
[Download RAW message or body]

On 5/30/06, David Faure <faure@kde.org> wrote:
> On Tuesday 30 May 2006 14:05, Tomas Mecir wrote:
> > On 5/29/06, Thomas Zander <zander@kde.org> wrote:
> > > In KWord I have a KWCanvas which implements both:
> > > class KWCanvas : public QWidget, public KoCanvasBase
> > >
> > > I instantiate it and then pass it to the setCanvas call.  (done in the
> > > KWGUI class) Obviously I need to downcast it either to QWidget or to
> > > KoCanvasBase.
> > >
> > > In the setCanvas method I then do a dynamic_cast.
> > > Problem is that if I downcast to a KoCanvasBase the dynamic cast to a
> > > QWidget fails.
> >
> > Not sure if I understand the problem correctly, but it sounds like you
> > need virtual inheritance - ie., class KWCanvas : virtual public
> > QWidget, virtual public KoCanvasBase
> > Or something like that. I'm not really sure whether it would help or
> > not
> It wouldn't. This only allows you diamond-shaped inheritance trees,
> which isn't really the problem at hand.

Ah, I see. But I still don't get the problem. I just tried with some
simple classes, and it seems to work properly.

Here is my little test-program:

#include <iostream>

using namespace std;

class A {
  public:
  virtual ~A(){};
  int a;
};

class B {
  public:
  virtual ~B(){};
  int b;
};

class C: public A, public B {
  public:
  virtual ~C(){};
  int c;
};

int main() {
  C *c = new C;
  A *a = dynamic_cast<A*>(c);
  B *b = dynamic_cast<B*>(a);
  cout << ((b != 0) ? "b is not NULL" : "b is NULL") << endl;
  return 0;
}

This outputs "b is not NULL", so it works like it should, at least for
me. I had to add those virtual destructors, because otherwise the
compilation failed on
test.cc:20: error: cannot dynamic_cast 'a' (of type 'class A*') to
type 'class B*' (source type is not polymorphic)

This all with gcc 4.0.2.
So, I either don't understand the problem, or it works properly here.

/ Tomas
_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic