C
cppaddict
I have a method that uses a fairly large object. The choice is
between having a local object in the method or a static member object
that the method uses.
------CHOICE 1---------
int MyClass::myMethod(int param) {
MyBigObject o();
//initialize o based on param
//do calculations with o
return calclulatedValue;
}
------CHOICE 2---------
int MyClass::myMethod(int param) {
const MyBigObject& o = m_staticMemberHoldingAMyBigObject;
//re-initialzie o based on param
// rest is same as above
}
How much extra work is involved in the creation of the local object
with each call of myMethod()? More precisely, what actually happens?
Is new memory allocated for the object each time? Is the memory
allocated on the stack? On a temporary stack which exists during the
method call? I'm trying to understand what goes on under the hood as
well as find an answer to the practical problem.
Is it also true that choice 1 would be thread safe, while choice 2
would not?
Thanks for any help,
cpp
between having a local object in the method or a static member object
that the method uses.
------CHOICE 1---------
int MyClass::myMethod(int param) {
MyBigObject o();
//initialize o based on param
//do calculations with o
return calclulatedValue;
}
------CHOICE 2---------
int MyClass::myMethod(int param) {
const MyBigObject& o = m_staticMemberHoldingAMyBigObject;
//re-initialzie o based on param
// rest is same as above
}
How much extra work is involved in the creation of the local object
with each call of myMethod()? More precisely, what actually happens?
Is new memory allocated for the object each time? Is the memory
allocated on the stack? On a temporary stack which exists during the
method call? I'm trying to understand what goes on under the hood as
well as find an answer to the practical problem.
Is it also true that choice 1 would be thread safe, while choice 2
would not?
Thanks for any help,
cpp