简体   繁体   English

如何在运行时使用静态成员函数初始化静态成员变量?

[英]How to initialize a static member variable using a static member function at runtime?

As the question states i'm trying to initalize a static class member variable using this same class's static member function but at a chosen time during runtime. 正如问题所指出的那样,我正在尝试使用同一类的静态成员函数来初始化静态类成员变量,但要在运行时的选定时间。

The is that the copy constructor for GameDataLocalResource is implicitly deleted because one of its fields has no copy assignement operator. 的原因是GameDataLocalResource的副本构造函数被隐式删除,因为其字段之一没有副本分配运算符。 So I tried to define the copy constructor but i'm still getting the same error at compilation. 所以我试图定义复制构造函数,但在编译时仍然遇到相同的错误。

How should i handle the problem. 我应该如何处理这个问题。 Please keep in mind i'm beginner in C++. 请记住,我是C ++的初学者。

I looked at many thread on how to initialize a static member variable at runtime but none seems to fit my situation. 我看了很多关于如何在运行时初始化静态成员变量的线程,但似乎没有一个适合我的情况。

//*.h file
class GameDataResource
{

private:
    static GameDataLocalResource local_resource;

public:
    static void initializeLocalResource();
    static GameDataLocalResource  getLocalResource();
}

//*.cpp file

void GameDataResource::initializeLocalResources()
{
    GameDataResource::local_resource = GameDataLocalResource();
}

GameDataLocalResource GameDataResource::getLocalResources()
{
    return GameDataResource::local_resource;
}

//main.cpp

int main(int argc, char *argv[])
{
...
    GameDataResource::initializeLocalResources();
    qDebug() << GameDataResource::getLocalResources().getLoadingPercentage();
...
}

I expect to get the value of loading percentage but instead i get: 我希望得到加载百分比的值,但我得到:

copy assignment operator of 'GameDataLocalResource' is implicitly deleted because field '****' has no copy assignment operator 因为字段“ ****”没有副本分配运算符,所以“ GameDataLocalResource”的副本分配运算符被隐式删除

GameDataLocalResource是可以初始化静态变量的类型而不是函数,可以解决您的问题。

So the solution I found was hinted in an answer that was deleted. 因此,我找到的解决方案在已删除的答案中得到了提示。 Mainly I added a new member function to GameDataLOCALResource that initializes its "heavy" members. 主要是我向GameDataLOCALResource添加了一个新的成员函数,该函数初始化其“大量”成员。

By doing so I able to make an instance of it without loading the files. 这样我就可以创建它的实例而无需加载文件。

Then a call to a static member function of GameDataResource triggers the GameDataLOCALResource instance to load the files into its member variables. 然后,对GameDataResource的静态成员函数的调用将触发GameDataLOCALResource实例,以将文件加载到其成员变量中。

Thanks everyone ! 感谢大家 !

Hello and welcome to C++ and stackoverflow. 您好,欢迎使用C++和stackoverflow。 Since you are new and trying to understand the concepts of something being static there are two versions. 由于您是新手,并且试图了解static事物的概念,因此有两个版本。 You can read up about them from these links: 您可以从以下链接中了解它们:

Since your question involves class members, you can focus more on the later. 由于您的问题涉及班级成员,因此您可以将重点放在后面。


How does static-members work? static-members如何工作? They do not belong to an object, they can be considered incomplete until a definition is encountered. 它们不属于对象,在遇到定义之前可以认为它们是不完整的。 The static keyword for class members can only be used during the declaration. 类成员的static关键字只能在声明期间使用。 The initialization of a class's static-member must be defined outside of the class. 类的static-member的初始化必须在类外部定义。


Here is a simple example of static member initialization: 这是静态成员初始化的一个简单示例:

SomeClass.h SomeClass.h

class Foo {
public:
    static int bar;
    int x;

    void someFunc();
};

Here when the class's cpp file is compiled Foo::bar has static duration and internal linkage. 在这里,当编译类的cpp文件时, Foo::bar具有静态持续时间和内部链接。 The static member has no association to the object of Foo but can be accessed by the class's this pointer for example: 静态成员与Foo的对象没有关联,但是可以通过类的this指针进行访问,例如:

SomeClass.cpp SomeClass.cpp

int Foo::bar = 0;

void Foo::someFunc() {
    this->x = 5; // okay
    this->bar = 9; // okay as an instance of this object can access `bar` 
                   // since all instances share this static member
                   // there is only ever one instance of `Foo::bar` in memory
}

To show that it has no association to the actual instance or an object of type Foo we can see this from the example below. 为了表明它与实际实例或Foo类型的对象没有关联,我们可以从下面的示例中看到这一点。

SomeOtherClassOrFunction SomeOtherClassOrFunction

{
    Foo f;
    f.a = 5; // okay as long as `a` is public
    f.bar = 9; // same as above `bar` is shared across all instances of Foo

    // Accessing bar we do not need an object we can do it as such:
    std::cout << Foo::bar << '\n'; // Should print 9.
}

Now that you have a general understanding of static member variables static functions follow similar rules except for the rules that govern how their address can be stored in a pointer, but that is beyond the scope of this topic. 既然您已经对static成员变量有了大致的了解,那么static函数遵循类似的规则,除了那些控制如何将其地址存储在指针中的规则之外,但这不在本主题的讨论范围之内。 The only major difference is static member functions can be accessed by the this-pointer but have no association to that object as they are static functions. 唯一的主要区别是this-pointer可以访问static member functions ,但它们与该对象没有关联,因为它们是静态函数。


We can take the above example and remove the non static member and change the storage class of its member function and rename it. 我们可以以上面的示例为例,删除非静态成员并更改其成员函数的存储类并重命名它。

Foo.h oo

#pragma once

class Foo {
public:
    static int bar;

    static void update(int val) { bar = val; }
};

Foo.cpp Foo.cpp

#include "Foo.h"

int Foo::bar = 0; // default initialized

main.cpp main.cpp

#include <iostream>
#include "Foo.h:"

int main() {
    std::cout << "Default Foo::bar = " << Foo::bar << '\n';

    Foo::update(25);

    std::cout << "Updated Foo::bar = " << Foo::bar << '\n';

    return 0;
}

I'm not sure if this is the exact behavior you are looking for, but this is the basic or general concepts and usages of static class members. 我不确定这是否是您要查找的确切行为,但这是静态类成员的基本或一般概念和用法。 Hopefully this will give you some insight. 希望这会给您一些见识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM