繁体   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