All objects must be initialised before we can use them. Initialisation can either occur statically (at compile-time) or dynamically (at runtime) however declaring an object static does not imply static initialisation.
Static initialisation relies on constant expressions. A constant expression is any expression that can be computed at compile-time. If a computation involves one or more function calls, those functions must be declared constant expression (constexpr) in order to take advantage of compile-time computation. However, whether or not the computation actually occurs at compile-time or not is implementation-defined. Consider the following code:
constexpr unsigned f (unsigned n) { return n<2?1:n*f(n-1); }
static unsigned num = f (4); // e.g., num = 4 * 3 * 2 * 1 = 24
int main () {
// ...
}
The above example is a potential candidate for compile-time computation. A good compiler will interpret this code as if it had been written:
static unsigned num = 24;
int main () {
// ...
}
However, if the function f () were not declared constexpr, the compiler would instead interpret the code as if it were actually written as follows:
unsigned f (unsigned n) { return n<2?1:n*f(n-1); }
static unsigned num; // uninitialised!
int main () {
num = f (4);
// ...
}
In other words, num is dynamically initialised -- at runtime.
Local objects are always initialised dynamically, as are objects allocated upon the heap (the free store). Static objects may be statically allocated, however this depends on the complexity of the object's constructor as well as the compiler's ability to perform compile-time computation.
The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code
Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.
Is an important thing to do.
No, C++ does not use dynamic memory management. The programmer is entirely responsible for releasing dynamic memory when it is no longer required. When static objects fall from scope, their destructors are called automatically, but there is no automatic garbage collection for dynamic objects. Allocated memory remains allocated until the programmer manually releases it, or the thread that owns the memory is terminated.
1.Classes and Objects 2.Constructors and Destructors 3.Inheritance 4.Polymorphism 5.Dynamic Binding
Instantiation is creating the instance of the variable/object . While Initialization is to provide the variable with some value. int i; // i is an instance of an integer i=10; //initialised with the value 10
Not initialized variable: int myInt; Initialized variable: int myInt = 10;
The C++ standard has this to say about dynamic initialisation:"Objects with static storage duration shall be zero-initialised before any other initialisation takes place. Zero-initialisation and initialisation with a constant expression are collectively called static initialisation; all other initialisation is dynamic initialisation."
Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.
Static binding occurs at compile time. Dynamic binding occurs at runtime.
You do not import classes. Classes are the definition of a type, and if you link to a library then all of its types are made available to your program. All you have to do is instantiate objects from those types: thus there is nothing to import.
Objects that are not supposed to be written. Surprised?