Q:

What are static members in C++?

0

What are static members in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

We are breaking this question into three-part because a static keyword has an important role in C++.

member variable as static (static member variable):

The static keyword allows a variable to maintain its value among different function calls. The value of static variable changes when the variable has been accessed, the variable keeps the new value. If the same variable gets accessed again, it would be holding its most recent value. This is possible because, when the static variable is declared, the compiler uses a separate memory area to store it (BSS or DS). By doing this, when the value of the static variable gets changed, it is updated in the memory it is occupying. And because this memory is separate, the compiler can monitor its values even when its function exits.

function as static (static member functions):

There are some points related to the static function.

  • A static member function can access only static member data, static member functions and data and functions outside the class.
  • A static member function can be called, even when a class is not instantiated.
  • A static member function cannot be declared virtual.
  • A static member function cannot have access to the ‘this’ pointer of the class.
  • A static member function does not have this pointer, so there is no meaning of using a CV qualifier (const, volatile, const volatile) with static member function because the cv-qualifier modifies the behavior of ‘this’ pointer.

destructor as static:

A “static destructor” is a static member function of the class that accepts one argument a pointer to the object of that class to be destroyed. It is probably used along with “a factory method”. When there is a need to restrict the creation of instances of some class to free store only and/or perform additional steps before or after the creation of an object. Similar steps may need to be taken before and/or after destroying an instance.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What do you mean by inline function and How to imp... >>
<< What is a member function in C++?...