简体   繁体   中英

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. 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++.

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是可以初始化静态变量的类型而不是函数,可以解决您的问题。

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.

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.

Thanks everyone !

Hello and welcome to C++ and stackoverflow. Since you are new and trying to understand the concepts of something being static there are two versions. 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? 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. The initialization of a class's static-member must be defined outside of the class.


Here is a simple example of static member initialization:

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. The static member has no association to the object of Foo but can be accessed by the class's this pointer for example:

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.

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. 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.


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

#pragma once

class Foo {
public:
    static int bar;

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

Foo.cpp

#include "Foo.h"

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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