簡體   English   中英

通過引用傳遞向量到另一個類/文件

[英]Passing vector by reference to another class/file

我有 2 套頭文件+源文件。 一個帶有主 GUI class,另一個帶有派生 GUI class(主 window 打開第二個窗口)。

在主 class 我有一個字符串向量。 我可以通過在 Derived class 中調用 function 並通過引用傳遞該向量。 我可以在此 function 中使用和更新該向量,並且更改將在主類/文件中可用。 到目前為止,一切都很好。

我想做的下一件事是在派生的 class 的所有函數中使用這個通過引用向量傳遞的。 到目前為止,我在一組“通用”標題+源中創建了“外部”向量。 這使它成為一個全局向量,雖然它可以工作,但它不是最優雅的方式。

是否有另一種方法可以使向量可用於派生 GUI 類/文件中的所有函數(以及稍后添加/編輯主 GUI 類/文件中可用的元素)?

主框架.h

class wxMainFrame: public GUIFrame
{
    public:
        wxMainFrame(wxFrame *frame);
        ~wxMainFrame();
        DerivedFrame *m_DerivedFrame;
    private:
        std::vector<wxString> vwsM3;
        ....etc
}

派生幀.h

class DerivedFrame: public OtherFrane
{
        public:
            DerivedFrame( wxWindow* parent );
            ~DerivedFrame();
        private:
            std::vector<wxString> vwsM4;
            void PassVector(std::vector<wxString> &vwsM);
            void USEvwsM();
            ....etc
}

主框架.cpp

wxMainFrame::wxMainFrame(wxFrame *frame) : GUIFrame(frame)
{
    m_DerivedFrame = new DerivedFrame(this);
    m_DerivedFrame->PassVector(&vwsM3);
}

派生幀.cpp

DerivedFrame::DerivedFrame ( wxWindow* parent ) : OtherFrame( parent )
{
    //
}

void DerivedFrame::PassVector(std::vector<wxString> &vwsM)
{
    vwsM.push_back("Something");
}

void USEvwsM()
{
    // ??
}

OnInit() (這里不知道向量 vwsM3,因為它在單獨的頭文件+源文件中)

IMPLEMENT_APP(wxMainApp);

bool wxMainApp::OnInit()
{
    wxMainFrame* frame = new wxMainFrame(0L);
    frame->SetIcon(wxICON(aaaa)); // To Set App Icon
    frame->Show();

    return true;
}

擁有全局矢量是不好的做法,但無論如何對於矢量這樣的設置來說都是典型的。

當我理解正確時,您要共享的向量在基礎中是這樣的

struct base {
    std::vector<std::string>& data;
    base(std::vector<std::string>& init) : data(init) {}
};

struct derived : base {
    derived(std::vector<std::string>& init) : base(init) {}
    void have_fun_with_VectorOfStrings();
};

它可以在派生的 class 或任何有權訪問派生的 class 之一的實體中直接訪問。

不確定您是否正在尋找不同的方法,例如singleton 模式

class coolStuff {
    public:
    std::vector<std::string> data;
    static coolStuff& get() {
        static coolStuff instance;
        return instance;
    }
    private:
    coolStuff () {
        // constructor called once using "get", so can be used for initialization
    }
};

這將在您需要的任何地方簡單地調用。 由於只存在一個實例,因此它可能是實現相同目標的更好方法。

coolStuff::get().data.push_back("add a new string");

同時,您共享了一個代碼示例,因此您的示例看起來像上面應用方法 1。

class wxMainFrame: public GUIFrame {
    public:
        wxMainFrame(wxFrame *frame, std::vector<wxString>& vwsM3);
    private:
        std::vector<wxString>& vwsM3;
};

wxServerFrame::wxServerFrame(wxFrame *frame, std::vector<wxString>& _vwsM3) : GUIFrame(frame)
, vwsM3(_vwsM3)
{
    m_DerivedFrame = new DerivedFrame(this, _vwsM3);
    // m_DerivedFrame->PassVector(&vwsM3); // not needed anymore
}
// same for further inherited classes

如果我可以補充一點:看起來你正在做一些類似圖形的東西,所以也應該考慮性能:盡量避免像 new、malcoc 等動態分配,因為這是一個非常慢的操作。 優化可能是使用 class 中的成員,而不是在運行時分配給成員指針。

  1. 為派生的 class 添加一個指針字段:
class DerivedFrame: public OtherFrame {
    .......
    private:
        std::vector<wxString> * pvwsM3 = nullptr;
    .......
}
  1. 修改 PassVector() 方法以填充指針:
void DerivedFrame::PassVector(std::vector<wxString> & vwsM) {
    pvwsM3 = &vwsM;
}
  1. 現在使用指針:
void DerivedFrame::USEvwsM() {
    assert(pvwsM3); // Check that we don't have null pointer, you may throw exception instead.
    pvwsM3->push_back("Something");
}
  1. 其余代碼與您的相同。 或者,您可以將向量傳遞給 DerivedFrame 的構造函數,這比單獨調用 PassVector() 更可靠(您可能忘記調用,而構造函數總是調用):
DerivedFrame::DerivedFrame(wxWindow* parent, std::vector<wxString> & vwsM)
    : OtherFrame( parent ) {
    this->PassVector(vwsM);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM