lundi 23 octobre 2017

Static member declaration c++11

I create a basic IBasic interface with a static field

class IBasic
{
public:
    IBasic();
    virtual ~IBasic();

    static std::vector< std::vector<char> > Field;
};

from which the Inherit class is inherited:

class Inherit : public IBasic
{
public:
    Inherit(int);
    ~Inherit();
    void Foo();
};

The Inherit class makes some manipulations with Field static member in constructor/or member function. In order to create an instance of the Inherit class, we need to explicitly declare a static field in the main.cpp before the main function:

#include "Basic.h"
#include "Inherit.h"

std::vector< std::vector<char> > IBasic::Field;

int main()
{
    Inherit(10);

    return 0;
}

The questions are:

  1. In what namespace does the static method actually exists (global?)? Because I know that static field/function is not a class member in fact.
  2. Is there another way to declare this static method, for example, in a class file, inside a main function, or through creation unnamed namespace? Is it only one right variant?
  3. How is right? What should be considered first of all?

Aucun commentaire:

Enregistrer un commentaire