user8424470
user8424470

Reputation:

C++ local initialization?

So there is something called static initialization and dynamic initialization and apparently they do not describe a certain way of initializing but when things are initialized. Static and dynamic initialization can only used to designate initializations of non-local variables...

So what about local variables? When do their initializations happen and what is it called? I cannot find anything called local initialization? I mean wouldn't it be quite convenient to have a name for when they are initialized since value-/ aggregate-/ etc. initializations describe what initialization happens and the can even be used with static and dynamic initialization(as far as I know) which makes it just a bit more confusing to me..

Hope this made somewhat sense to you :)

Upvotes: 1

Views: 124

Answers (3)

sampathfit
sampathfit

Reputation: 1

Simple logic is all local variables (static or dynamic) get initiated when only they are called in run time.

class Test
{
public : 
    Test(string text)
    {
        cout << (text) << endl;
    }
};
void print()
{
    Test t1("local");
    static Test t2 ("local static");
}
    int main(int argc, char* argv[]) {
    cout << "begin" << endl;
    print();
    cout << "end" << endl;
}

anwers

begin
local
local static
end

Upvotes: 0

User420
User420

Reputation: 50

Local variables are initialized when the scope is entered/reentered. There is no specific terminology in c++ for local variable initialization.

Consider following example

for (int i = 0; i < 5; ++i) {
    int n = 0;
    printf("%d ", ++n);  
     /* prints 1 1 1  - the previous value is lost
        every time n is initialized with 0 when scope is entered
     */
}

Upvotes: 0

molbdnilo
molbdnilo

Reputation: 66371

Local variables are initialised when they are constructed.
When doesn't need a name, only what is interesting.

Upvotes: 0

Related Questions