Problem mit virtual und polymorphi



  • Hi,
    folgendes Prob:
    In ner Basisklasse (Widget) ist ne virutelle Methode deffiniert (getParentWorld()). In der Abgeleiteten Klasse (World) ist sie ueberschrieben.

    Wenn ich jetzt im Hauptprogramm (main()) einen Widget* pointer auf ein World objekt habe und ueber den pointer->getParentWorld() von der World aufrufen will, wird die Methode vom Widget und nicht von der World ausgefuehrt.

    Ich hoffe, dass das verstaendlich ist, hier noch der Code:

    main.cpp

    #include "widget.h"
    #include "world.h"
    
    #include <iostream>
    using std::cerr;
    using std::endl;
    
    using calina::Widget;
    using calina::World;
    
    int main() {
    	World p("world");
    	Widget ch1(&p, "ch1");
    
    	ch1.getParentWorld();
    
    	cerr << "=============================" << endl;
    
    	Widget* test = &p;
    	test->getParentWorld();
    
    	return 0;
    /*
    Ausgabe:
    ch1=> Widget::getParentWorld()  parentWidget=0xbff81dbc
    world=> Widget::getParentWorld()        parentWidget=0
    =============================
    world=> Widget::getParentWorld()        parentWidget=0
    
    Gewuenschte Ausgabe:
    ch1=> Widget::getParentWorld()  parentWidget=0xbff81dbc
    world=> Widget::getParentWorld()        this=0xbf964fa8
    =============================
    world=> Widget::getParentWorld()        this=0xbf964fa8
    */
    }
    

    widget.h

    #ifndef CALINAWIDGET_H
    #define CALINAWIDGET_H
    
    #include <QObject>
    #include <QString>
    
    namespace calina {
    class World;
    class Widget : public QObject {
    	Q_OBJECT
    public:
    	Widget(Widget *parent, QString name="");
    	virtual ~Widget();
    	QString getName() const;
    	void setName(QString name);
    	virtual World* getParentWorld() const;
    	Widget* getParent() const;
    private:
    };
    }
    
    #endif
    

    world.h

    #ifndef CALINAWORLD_H
    #define CALINAWORLD_H
    
    #include "widget.h"
    
    class QGLWidget;
    
    namespace calina {
    
    class World : public virtual Widget {
    public:
    	World(QString name="World");
    	~World();
    	virtual World* getParentWorld();
    };
    
    }
    
    #endif
    

    world.cpp

    #include "world.h"
    
    #include <iostream>
    using std::cerr;
    using std::endl;
    
    namespace calina {
    
    World::World(QString name)
    	: Widget(0, name) {
    }
    
    World::~World() {
    }
    
    World* World::getParentWorld() {
    	cerr <<  getName().toStdString() <<
    			"=> Widget::getParentWorld()\tthis=" << this << endl;
    	return this;
    }
    
    }
    

    Ich verwende die Qt Library, hier die Dokumentation zu QObject:
    http://doc.trolltech.com/4.2/qobject.html

    Ich hab da relativ lang gesucht, aber den Bock nicht gefunden. Ist sicher etwas ganz einfaches..



  • hoi

    virtual World* getParentWorld() const; // <- const vergessen ?
    

    Meep Meep



  • Danke, das wars..


Anmelden zum Antworten