Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const variable in C++ function body

Tags:

c++

I believe I have it understod but I just want confirmation. If I have two functions,

function A()
{
    const Vector3D vectorA(1.0f);
    ...
}

function B(float var)
{
    const Vector3D vectorB(1.0f + var);
    ...
}

In the case of function A(), will vectorA only be constructed once in the program, no matter how many A() calls? I believe the compiler implicitly declares it static yes? But in the case of B(), vectorB needs to be reconstructed each function call?

like image 975
KaiserJohaan Avatar asked Dec 20 '22 20:12

KaiserJohaan


1 Answers

Both functions will create the variables every time they are called. There is no implicit static. Some compilers may choose to optimize but it is not part of the language specification.

like image 197
TimDave Avatar answered Jan 01 '23 23:01

TimDave