初始化全局变量类

发布于 2024-12-12 02:39:51 字数 258 浏览 0 评论 0原文

对于这样一个基本问题表示歉意,但我无法弄清楚。我知道你可以像这样初始化一个类:

QFile file("C:\\example");

但是你如何从全局变量初始化它呢?例如:

QFile file; //QFile class

int main()
{
    file = ?? //need to initialize 'file' with the QFile class
}

Apologies for such a basic question but I can't figure it out. I know you can initialize a class like this:

QFile file("C:\\example");

But how would you initialize it from a global variable? For example:

QFile file; //QFile class

int main()
{
    file = ?? //need to initialize 'file' with the QFile class
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

胡渣熟男 2024-12-19 02:39:51

1. 直接回答

如果类是可分配/可复制构造的,您可以只写

QFile file; //QFile class

int main()
{
    file = QFile("C:\\example");
}

2. 使用间接

如果不是,您将不得不求助于其他选项:

QFile* file = 0;

int main()
{
    file = new QFile("C:\\example");

    //
    delete file;
}

或使用 boost::optionalstd::shared_ptrboost::scoped_ptr

3. 使用单例相关模式:

由于 静态初始化惨败,您可能想要编写这样一个函数:

static QFile& getFile()
{ 
    static QFile s_file("C:\\example"); // initialized once due to being static
    return s_file;
}

C++11 也使这样一个函数局部静态初始化线程安全(引用 C++0x 草案 n3242,§6.7:)

在此处输入图像描述

1. Straightforward answer

If the class is assignable/copy constructible you can just write

QFile file; //QFile class

int main()
{
    file = QFile("C:\\example");
}

2. Use indirection

If not, you'll have to resort to other options:

QFile* file = 0;

int main()
{
    file = new QFile("C:\\example");

    //
    delete file;
}

Or use boost::optional<QFile>, std::shared_ptr<QFile>, boost::scoped_ptr<QFile> etc.

3. Use singleton-related patterns:

Due to the Static Initialization Fiasco you could want to write such a function:

static QFile& getFile()
{ 
    static QFile s_file("C:\\example"); // initialized once due to being static
    return s_file;
}

C++11 made such a function-local static initialization thread safe as well (quoting the C++0x draft n3242, §6.7:)

enter image description here

老娘不死你永远是小三 2024-12-19 02:39:51

同样的方式:

// file.cpp

QFile file("C:\\example");

int main()
{
  // use `file`
}

所有全局对象的构造函数在调用 main() 之前执行(对于析构函数则相反)。

The same way:

// file.cpp

QFile file("C:\\example");

int main()
{
  // use `file`
}

The constructors of all global objects execute before main() is invoked (and inversely for destructors).

一口甜 2024-12-19 02:39:51
#include <iostream>
struct QFile
{
    char const* name;
    QFile(char const* name) : name(name) {}
};

QFile file("test.dat"); // global object

int main()
{
    std::cout << file.name << std::endl;
}

顺便说一句,你写道:

我知道你可以初始化一个类

你不能初始化一个类。您创建并初始化对象,而不是类。

#include <iostream>
struct QFile
{
    char const* name;
    QFile(char const* name) : name(name) {}
};

QFile file("test.dat"); // global object

int main()
{
    std::cout << file.name << std::endl;
}

By the way, you wrote:

I know you can initialize a class

You can not initialize a class. You create and initialize object, not a class.

々眼睛长脚气 2024-12-19 02:39:51

是否使用全局变量或其他变量完全取决于您。

对于全局变量,您可以在文件中全局定义它,然后在函数中初始化它,如下所示,请确保正确编写复制构造函数。

QFile file; //class object

int main()
{
    file = QFile("C:\\example"); //create a temporary object and copy it to `file`
}

但是,如果您使用全局指针会更好,

QFile *file;

int main()
{
    file = new QFile("C:\\example");
}

请注意,如果您仅在文件内使用此全局变量,则可以将其设为静态变量。那么它就仅限于文件本身。因此您可以最大限度地减少全局变量的

It is totally up to you to decide whether you use a global variable or some thing else.

For a global variable you can define it in globally in the file and then initialize it in function as below, make sure that you write copy constructor properly.

QFile file; //class object

int main()
{
    file = QFile("C:\\example"); //create a temporary object and copy it to `file`
}

But, it is better if you use global pointer,

QFile *file;

int main()
{
    file = new QFile("C:\\example");
}

Note, if you are using this global variable only within the file, you can make it a static variable. Then it is limited to file itself. So you can minimize the bad of global variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文