开发者

What is the relationship between a static function of a class and the constructor of the class?

Wh开发者_开发百科at happens every time a static function is called on the class? When is the constructor executed?


There is no dependency relationship, that is, you can call the static method without constructing an instance with the syntax "Class::method()"

class someclass
{
public:
    someclass()
    {
        std::cout << "someclass constructor" << std::endl;
    }

    static void staticmethod()
    {
        std::cout << "someclass staticmethod" << std::endl;
    }
};

and then

int main(int argc, char** argv)
{
    someclass::staticmethod();
    return 0;
}

you will see printed out "someclass staticmethod" with no mention or need for invoking the constructor.


Constructors are called when the object is created.

static methods of a class are basically instant independent. In other words, the method does not keep values for individual objects they are shared amongst all the objects


Background on static member functions

static member functions can be called at any time, even if you haven't created an object instance of the class. When they're called:

  • they do not receive an implicit this pointer the way non-static member functions do
    • so, they're not implicitly automatically aware if there are any instances of the class, let alone where in memory instances are.
  • they can work on the static member variables, which are likewise independent of object instances.

To understand the above, it may help to imagine that the static members of a class or struct are similar to freestanding non-members except that:

  • static members effectively have friendship with the class
  • static members are inside the class's scope for the purposes of looking up the identifiers to match function calls or find the variable
  • static members can be protected or private within that scope

So, they're a hybrid of non-member and member behaviours.

How object construction relates to static/non-static members and threading

The constructor of objects can utilise static members of the class, either calling functions or using variables. But, remember that the static member variable is like a single global variable except that it's in the class's naming scope: any code using the variable - whether that code's a static or non-static member function or a non-member function - must cooperate with other code using the variable because the value is shared. If you write multi-threaded code, you'll need to use a mutex or similar to protect it exactly as you would for a non-member variable.

Construction of static member variables

There is one crucial issue that I think you may intend by the question: when is the constructor for the static member variables called? According to the Standard 9.4.2 (I'm referencing the C++98 final draft here):

-7- Static data members are initialized and destroyed exactly like non-local objects (basic.start.init, basic.start.term).

Crucially, the worst/latest-case scenario is described in 3.6.2:

-3- It is implementation-defined whether or not the dynamic initialization (dcl.init, class.static, class.ctor, class.expl.init) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.*


There is only one copy of the 'static' function for the class in which it is defined... i.e. all the objects of that class share the same 'static' function.

The constructor is only called when the object of that class is created. In case of c++, the constructor is called when you create an object of that class with the new operator.

To call a static method you need to use the Scope Resolution operator (::) and qualify the method name with the class name... However, in case of the constructor, it gets automatically invoked with the "new" operator.

Hope this helps.


The constructor is executed when an object is instantiated.


  • For class methods, static means that this method can be called on the class itself, no instance of that class is necessary to use the method
  • For functions in a basic C/C++ program, static functions are functions that are only visible to other functions in the same file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜