I have a C++ class Foo
that has a private member variable bar
that is only used once by Foo
's constructor and is then unused afterwards for the lifetime of a Foo
object, i.e. no other method of Foo
uses bar
. Assume that bar
is a rather large object. Assume also that there is a reason why bar
needs to be a member of Foo
even though it is used only in its constructor.
My question: Will bar
exist in memory for the lifetime of a Foo
object, or will the compiler free up the memory bar
occupies after construction of the Foo
object?
Note: There is this similar question on entirely unused member variables, but the answer for member variables that are used at least once may or may not be different, so I deemed it worth a separate question.