English 中文(简体)
推迟初始?
原标题:delayed initialization?

有些人可以解释为什么发生这种情况? 我写道:

(1) 窗口管理类别,作为单一州实施,有<条码>(Instance()方法,其定义如下:

static WindowManager * instance_;
//...
WindowManager * WindowManager::instance_ = 0;
WindowManager & WindowManager::Instance()
{
    if (!instance_)
        instance_ = new WindowManager();
    return *instance_;
}

2) aWindowManager:createWindow 回归提及新建立的窗口的方法,如:

Window & WindowManager::createWindow()
{
    windows_.push_back(Window());
    return windows_.at(windows_.size() - 1);
}

3)a 在窗口内打印信息的方法

在我的主要方案中,我撰写了以下文件:

ui::Window & win1 = ui::WindowManager::Instance().createWindow();
ui::Window & win2 = ui::WindowManager::Instance().createWindow();
win1.print("First window");
win2.print("Second window");

This does not work! Only the second call to print is executed (for win2). However, if I change the order, like so:

ui::Window & win1 = ui::WindowManager::Instance().createWindow();
win1.print("First window");
ui::Window & win2 = ui::WindowManager::Instance().createWindow();
win2.print("Second window");

那么,所有事情都是按预期运作的。 如果任何人能够说明这种情况,将非常感谢任何帮助。

最佳回答

在这里,你有迅速的例子说明问题:

#include <iostream>
#include <vector>
using namespace std;

struct T
{
    int id;
    T(int id) : id(id)
       { cout << "created " << id << endl; }
    T(T const& t) : id(t.id)
       { cout << "copy: " << t.id << endl; }
    void print(char const* m)
       { cout << id << ": " << m << endl; }
};

vector<T> ts;

T& create(int id)
{
    ts.push_back(T(id));
    return ts.at(ts.size() - 1);
}

int main()
{
    // Uncomment these lines and compare results
    //int const max_windows = 10;
    //ts.reserve(max_windows);

    T& t1 = create(1);
    T& t2 = create(2);
    t1.print("t1");
    t2.print("t2");
}

Compile and run as it is and see what is printed to stdout. Then uncomment reserve() call, compile and run once again and compare with previous output.

问题是<代码>std:vector:push_back造成数据内部重新分配。 这使你在重新定位之前获得的病媒要素的所有参考资料、点或探测器都失效。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签