Singleton and static member

3

Good morning, I do not understand the following line of code:

Boss* Boss::s_bossInstance = nullptr;

s_bossInstance is a private property of class Boss , then,

1. How is it possible that I can access it outside the same class?

2. Why should I have a return data type, if it is simply a property ... (I am not saving its contents, I am assigning it to it)

3. Why should it be outside the function main yes or yes?

 class Boss {

    private:
        static Boss* s_bossInstance;

    public:

        static Boss& Get() {
            return *s_bossInstance;
        }

    };

    Boss* Boss::s_bossInstance = nullptr;

The following is another example:

struct Entity {

    static int x,y;

    void Print() {
        std::cout << x << "," << y << std::endl;
    }

};

int Entity::x;//Estas lineas de código no entiendo porque nececitan estar
int Entity::y;

int main() {

    Entity e1;
    e1.x = 2;
    e1.y = 3;

    Entity e2;
    e2.x = 5;
    e2.y = 6;



    return 0;
}
    
asked by MatiEzelQ 09.10.2017 в 17:39
source

1 answer

2
  

How can I access it outside the same class?

You can not access private members of a class. But, you can create a public function to access them from the outside, which you are doing here:

public:

    static Boss& Get() {
        return *s_bossInstance;
    }

However the line:

Boss* Boss::s_bossInstance = nullptr;

It is not access to the data s_bossInstance , it is a definition of it.

  

Why should I have a return data type, if it is simply a property?

In C ++ everything has a type, there can not be a variable without type ... let's break down your statement:

//  +---- Tipo
//  |     +--- Ámbito
//  |     |
// vvvv  vvvv                   vvvvvvv <--- Inicializador.
   Boss* Boss::s_bossInstance = nullptr;
 //    ^       ^^^^^^^^^^^^^^ <--- nombre
 //    |
 //    +---- Puntero

Your statement says " Define an object of type pointer to Boss with the name s_bossInstance within the scope Boss and assign it the value nullptr ", if you did not indicate the type, it would not compile.

  

Why should it be outside the main function if or if?

I guess you want to know why the definition of a static member of a class should be outside the class (no relation to the main function).

We have to distinguish three separate concepts when creating elements in C ++, these concepts are:

  • Statement : Tells the compiler that something exists without going into detail about what it is like.
  • Definition : Describe something to the compiler in as much detail as possible, if it was not previously stated, the definition considers both things.
  • Linked : The process performed by the linker that consists of matching each statement with its definition.

This separation between declaration and definition allows C ++ to separate the code in header files and code files or optimize compilation using pre-statements .

It also forces you to make certain decisions such as:

  • Define everything that is used : if you use something declared without defining it, you will get a link error usually written as " External symbol without solving ".
  • Define one time only ( single definition rule ) : if something is defined more than once , even if all the definitions are the same, the linker will not know what definition to use and will also fail to link.

In the case of a static member of a class, its declaration is encompassed within the scope of the object to which it belongs but lacks definition; that is to say: the compiler knows that it exists but does not know how it is ... to tell the compiler how this static object is, you must define it and this is done outside the object that encompasses it:

class Boss {
    // declaracion: s_bossInstance existe y es de tipo Boss*.
    static Boss* s_bossInstance; 
};

// definicion: s_bossInstance existe, es de tipo Boss* y su valor es nullptr.
Boss* Boss::s_bossInstance = nullptr; 

Broadly speaking, a static member of an object is not linked to any instance of the object, so its statement is on the object itself but its definition must be external since otherwise, if the object Boss (which would include the definition of s_bossInstance ) is declared several times (for example: if it is in a header and this header is included in several files) there would be an ambiguity when trying to link.

    
answered by 09.10.2017 в 17:54