“不能”移动指针

时间:2011-12-19 07:25:37

标签: c++

在我的代码中,我有:

template<class Ui_Dialog>
QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *new_caller)
{
 QDialog* old_caller = caller_;

 caller_ = new_caller;//Here I'm trying to set this to new caller

 return old_caller;
} 

但是在将调用者设置为新调用者并退出此fnc后,当我调用调用者时,我仍然使用旧调用者而不是新调用者,就像没有进行任何更改一样。为什么呢?
编辑:

//caller is defined in a following way:  
    class Main_Dialog : public Base_Dialog<Ui::Main_Dialog> {};  

编辑2:
有趣的是,如果不是公共Base_Dialog,我将Base_Dialog更改为非模板并将Main_Dialog定义为:
        class Main_Dialog : public Base_Dialog, private Ui::Main_Dialog {};
然后它按预期工作。为什么?
编辑3:

class Main_Dialog;
    template<class Ui_Dialog>
    class Base_Dialog : public QDialog, protected Ui_Dialog
    {
       // Q_OBJECT //no signals/slots
    protected:
        Main_Dialog* main_dlg_;
        QDialog* caller_;

    public:
        Base_Dialog(Main_Dialog *main_dlg, QDialog *caller, QWidget *parent = nullptr);
        QDialog* set_caller(QDialog *);
        QDialog* clear_caller();
        Main_Dialog* clear_main_dlg();

    };

    /*----------------*/

        #include "Main_Dialog.hpp"
        template<class Ui_Dialog>
        Base_Dialog<Ui_Dialog>::Base_Dialog(Main_Dialog *main_dlg,QDialog *caller, QWidget *parent):
            QDialog(parent),
            main_dlg_(main_dlg),
            caller_(caller)
        {
            //setupUi(this);
        }


        #include <QtDebug>
        template<class Ui_Dialog>
        QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *new_caller)
        {
         QDialog* old_caller = caller_;

         caller_ = new_caller;

         return old_caller;
        }  
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
class Main_Dialog : public Base_Dialog<Ui::Main_Dialog>
{
    Q_OBJECT
    QSet<QDialog*>* dialogs_;
private:
    template<class Dialog,class Caller>
    bool already_created_(Caller*&, QDialog*& already_exists)const;

    template<class Dialog,class Caller, class Parent>
    QDialog* create_(Caller*,Parent*);

    /*template<class Dialog>
    void show_();*/

   /* template<class Dialog,class Caller>
    QDialog* find_(Caller*)const;*/
public:
    explicit Main_Dialog(QWidget *parent = 0);
    template<class Dialog,class Caller>
    QDialog* get_dialog(Caller*& caller);
public slots:
    void _1clicked();
    void _2clicked();
};

template<class Dialog,class Caller>
bool Main_Dialog::already_created_(Caller*& caller,QDialog*& already_exists)const
{/*the already_exists is introduced here in order to remove repetions of code and
   searching*/
    auto beg = dialogs_->begin();
    auto end = dialogs_->end();
    while(beg != end)
    {
        if(dynamic_cast<Dialog*>(*beg))
        {
            already_exists = *beg;
            static_cast<Base_Dialog*>(already_exists)->set_caller(caller);
            return true;
        }

        ++beg;
    }
    return false;
}



    template<class Dialog,class Caller, class Parent>
    QDialog* Main_Dialog::create_(Caller *caller, Parent *parent)
    {
        return (*dialogs_->insert(new Dialog(this,caller,parent)));
    }

    template<class Dialog,class Caller>
    QDialog* Main_Dialog::get_dialog(Caller *&caller)
    {
        QDialog* already_exists = nullptr;
        if (already_created_<Dialog>(caller,already_exists))
        {
            return already_exists;
        }
        else
        {
            return create_<Dialog>(caller,this);
        }  
    #include "Base_Dialog.hpp"
    #include "ui__1Dialog.h"

    class Main_Dialog;
    class _1Dialog : public Base_Dialog<Ui::_1Dialog>
    {
        Q_OBJECT
    public:
        explicit _1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent = 0);

    private slots:
        void _2clicked();
        void caller_clicked();
        void main_clicked();
    };

    #endif // _1DIALOG_HPP
    #include "_1Dialog.hpp"
    #include "_2Dialog.hpp"
    #include "Main_Dialog.hpp"
    _1Dialog::_1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent) :
        Base_Dialog(main_dlg,caller,parent)
    {
        setupUi(this);
    }

    void _1Dialog::_2clicked()
    {
        this->hide();
        main_dlg_->get_dialog<_2Dialog>(this)->show();
    }


    void _1Dialog::caller_clicked()
    {
        this->hide();
        caller_->show();
    }

    void _1Dialog::main_clicked()
    {
        this->hide();
        main_dlg_->show();
    }

    }
    #endif // MAIN_DIALOG_HPP

1 个答案:

答案 0 :(得分:1)

我认为你的问题不在set_caller中。接下来对我有用:

#include <iostream>

using namespace std;

class QDialog { 
        int i;
    public:
        QDialog() : i(0) {}
        QDialog(int i) : i(i) {}
        void me() { cout << i << endl; }
};

namespace UI {
    class Main_Dialog {}; // EDITED
}    

template<class Ui_Dialog> 
class Base_Dialog : public QDialog, protected Ui_Dialog
{
    QDialog* caller_;
public:
    QDialog* set_caller(QDialog *new_caller);
    QDialog* get_caller() {return caller_; } ;
};

template<class Ui_Dialog>
QDialog* Base_Dialog< Ui_Dialog>::set_caller(QDialog *new_caller)
{
 QDialog* old_caller = caller_;

 caller_ = new_caller;//Here I'm trying to set this to new caller

 return old_caller;
} 

class Main_Dialog : public Base_Dialog<UI::Main_Dialog> {
public:
};

int main()
{
    QDialog q1(1);
    QDialog q2(2);

    Main_Dialog md;

    md.set_caller(&q1);
    md.get_caller()->me();
    md.set_caller(&q2);
    md.get_caller()->me();


    return 0;
}