Class variable with scope restricted to functionWhat are the differences between a pointer variable and a reference variable in C++?Unresolved external symbol on static class membersDoes delete on a pointer to a subclass call the base class destructor?Why do we need virtual functions in C++?Variable Scoping in a method and its persistence in C++What is difference between instantiating an object using new vs. withoutCan a local variable's memory be accessed outside its scope?The static keyword and its various uses in C++Pointers to a class within void functionOpenCL context scope for class member?
Do they have Supervillain(s)?
Algorithms vs LP or MIP
Would the Republic of Ireland and Northern Ireland be interested in reuniting?
Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?
Does norwegian.no airline overbook flights?
Nothing like a good ol' game of ModTen
Heyacrazy: Careening
Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?
Anatomically Correct Whomping Willow
How to gently end involvement with an online community?
Was there ever a treaty between 2 entities with significantly different translations to the detriment of one party?
How to make Ubuntu support single display 5120x1440 resolution?
Why do all fields in a QFT transform like *irreducible* representations of some group?
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
Avoiding racist tropes in fantasy
Why isn't "I've" a proper response?
Disambiguation of "nobis vobis" and "nobis nobis"
Pythagorean triple with hypotenuse a power of 2
Thank God it's Friday, tomorrow is THE weekend. Why the definite article?
How do you harvest carrots in creative mode?
Are there any elected officials in the U.S. who are not legislators, judges, or constitutional officers?
How to respectfully refuse to assist co-workers with IT issues?
How do I get toddlers to stop asking for food every hour?
Why do gliders have bungee cords in the control systems and what do they do? Are they on all control surfaces? What about ultralights?
Class variable with scope restricted to function
What are the differences between a pointer variable and a reference variable in C++?Unresolved external symbol on static class membersDoes delete on a pointer to a subclass call the base class destructor?Why do we need virtual functions in C++?Variable Scoping in a method and its persistence in C++What is difference between instantiating an object using new vs. withoutCan a local variable's memory be accessed outside its scope?The static keyword and its various uses in C++Pointers to a class within void functionOpenCL context scope for class member?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
A member function will be called repeatedly.
I suppose it is time consuming to allocate memory to local variables every time.
A solution would be to declare these variables in the class declaration.
But I do not want them to be visible to other functions of the same class.
Is there a way to do that?
Tried to use namespaces, but it didn't work.
class A
int n; // only function 'One' should see 'n'
public:
A(void); // default constructor
int One(void); // member function
int Two(void); // member function
;
A::A(void) // default constructor
n = 7;
int A::One(void) // function 'One'
return n; // no time lost declaring 'n'
int A::Two(void) // function 'Two'
int n = 7; // obligatory, no other 'n' available
return n;
c++
add a comment |
A member function will be called repeatedly.
I suppose it is time consuming to allocate memory to local variables every time.
A solution would be to declare these variables in the class declaration.
But I do not want them to be visible to other functions of the same class.
Is there a way to do that?
Tried to use namespaces, but it didn't work.
class A
int n; // only function 'One' should see 'n'
public:
A(void); // default constructor
int One(void); // member function
int Two(void); // member function
;
A::A(void) // default constructor
n = 7;
int A::One(void) // function 'One'
return n; // no time lost declaring 'n'
int A::Two(void) // function 'Two'
int n = 7; // obligatory, no other 'n' available
return n;
c++
3
" I suppose it is time consuming to allocate memory to local variables every time." Is this actually true? Have you tested that?
– Jodocus
Mar 27 at 17:41
Sounds like a XY-problem for me. What's the reason for this requirement: "But I do not want them to be visible to other functions of the same class."? However such could be probably achieved with inheritance constructs.
– πάντα ῥεῖ
Mar 27 at 17:47
@user11132556 " I suppose it is time consuming to allocate memory to local variables every time." It's usually not. Please don't do premature that kindo micro optimization, before you actually hit a performance bottleneck with your application when local variables are allocated. If so it might be rather a problem with those constructor functions, but not really the allocation of the variable representation itself.
– πάντα ῥεῖ
Mar 27 at 18:01
Good question. I had not tested, but now I did. In fact, it really takes time to allocate memory to local variables. But the subsequent function operations probably will take much more time. Surprisingly (to me), in some tests, repeated local variable declarations run faster than once outside function ones. Maybe reading values stored in local variables takes less time, I don't know.
– user11132556
Mar 27 at 19:05
@user11132556 I supposed you have not tested that in a release build with optimisation enabled and stuff? It's almost useless to test debug builds.
– Guillaume Racicot
Mar 27 at 20:49
add a comment |
A member function will be called repeatedly.
I suppose it is time consuming to allocate memory to local variables every time.
A solution would be to declare these variables in the class declaration.
But I do not want them to be visible to other functions of the same class.
Is there a way to do that?
Tried to use namespaces, but it didn't work.
class A
int n; // only function 'One' should see 'n'
public:
A(void); // default constructor
int One(void); // member function
int Two(void); // member function
;
A::A(void) // default constructor
n = 7;
int A::One(void) // function 'One'
return n; // no time lost declaring 'n'
int A::Two(void) // function 'Two'
int n = 7; // obligatory, no other 'n' available
return n;
c++
A member function will be called repeatedly.
I suppose it is time consuming to allocate memory to local variables every time.
A solution would be to declare these variables in the class declaration.
But I do not want them to be visible to other functions of the same class.
Is there a way to do that?
Tried to use namespaces, but it didn't work.
class A
int n; // only function 'One' should see 'n'
public:
A(void); // default constructor
int One(void); // member function
int Two(void); // member function
;
A::A(void) // default constructor
n = 7;
int A::One(void) // function 'One'
return n; // no time lost declaring 'n'
int A::Two(void) // function 'Two'
int n = 7; // obligatory, no other 'n' available
return n;
c++
c++
asked Mar 27 at 17:38
user11132556user11132556
11 bronze badge
11 bronze badge
3
" I suppose it is time consuming to allocate memory to local variables every time." Is this actually true? Have you tested that?
– Jodocus
Mar 27 at 17:41
Sounds like a XY-problem for me. What's the reason for this requirement: "But I do not want them to be visible to other functions of the same class."? However such could be probably achieved with inheritance constructs.
– πάντα ῥεῖ
Mar 27 at 17:47
@user11132556 " I suppose it is time consuming to allocate memory to local variables every time." It's usually not. Please don't do premature that kindo micro optimization, before you actually hit a performance bottleneck with your application when local variables are allocated. If so it might be rather a problem with those constructor functions, but not really the allocation of the variable representation itself.
– πάντα ῥεῖ
Mar 27 at 18:01
Good question. I had not tested, but now I did. In fact, it really takes time to allocate memory to local variables. But the subsequent function operations probably will take much more time. Surprisingly (to me), in some tests, repeated local variable declarations run faster than once outside function ones. Maybe reading values stored in local variables takes less time, I don't know.
– user11132556
Mar 27 at 19:05
@user11132556 I supposed you have not tested that in a release build with optimisation enabled and stuff? It's almost useless to test debug builds.
– Guillaume Racicot
Mar 27 at 20:49
add a comment |
3
" I suppose it is time consuming to allocate memory to local variables every time." Is this actually true? Have you tested that?
– Jodocus
Mar 27 at 17:41
Sounds like a XY-problem for me. What's the reason for this requirement: "But I do not want them to be visible to other functions of the same class."? However such could be probably achieved with inheritance constructs.
– πάντα ῥεῖ
Mar 27 at 17:47
@user11132556 " I suppose it is time consuming to allocate memory to local variables every time." It's usually not. Please don't do premature that kindo micro optimization, before you actually hit a performance bottleneck with your application when local variables are allocated. If so it might be rather a problem with those constructor functions, but not really the allocation of the variable representation itself.
– πάντα ῥεῖ
Mar 27 at 18:01
Good question. I had not tested, but now I did. In fact, it really takes time to allocate memory to local variables. But the subsequent function operations probably will take much more time. Surprisingly (to me), in some tests, repeated local variable declarations run faster than once outside function ones. Maybe reading values stored in local variables takes less time, I don't know.
– user11132556
Mar 27 at 19:05
@user11132556 I supposed you have not tested that in a release build with optimisation enabled and stuff? It's almost useless to test debug builds.
– Guillaume Racicot
Mar 27 at 20:49
3
3
" I suppose it is time consuming to allocate memory to local variables every time." Is this actually true? Have you tested that?
– Jodocus
Mar 27 at 17:41
" I suppose it is time consuming to allocate memory to local variables every time." Is this actually true? Have you tested that?
– Jodocus
Mar 27 at 17:41
Sounds like a XY-problem for me. What's the reason for this requirement: "But I do not want them to be visible to other functions of the same class."? However such could be probably achieved with inheritance constructs.
– πάντα ῥεῖ
Mar 27 at 17:47
Sounds like a XY-problem for me. What's the reason for this requirement: "But I do not want them to be visible to other functions of the same class."? However such could be probably achieved with inheritance constructs.
– πάντα ῥεῖ
Mar 27 at 17:47
@user11132556 " I suppose it is time consuming to allocate memory to local variables every time." It's usually not. Please don't do premature that kindo micro optimization, before you actually hit a performance bottleneck with your application when local variables are allocated. If so it might be rather a problem with those constructor functions, but not really the allocation of the variable representation itself.
– πάντα ῥεῖ
Mar 27 at 18:01
@user11132556 " I suppose it is time consuming to allocate memory to local variables every time." It's usually not. Please don't do premature that kindo micro optimization, before you actually hit a performance bottleneck with your application when local variables are allocated. If so it might be rather a problem with those constructor functions, but not really the allocation of the variable representation itself.
– πάντα ῥεῖ
Mar 27 at 18:01
Good question. I had not tested, but now I did. In fact, it really takes time to allocate memory to local variables. But the subsequent function operations probably will take much more time. Surprisingly (to me), in some tests, repeated local variable declarations run faster than once outside function ones. Maybe reading values stored in local variables takes less time, I don't know.
– user11132556
Mar 27 at 19:05
Good question. I had not tested, but now I did. In fact, it really takes time to allocate memory to local variables. But the subsequent function operations probably will take much more time. Surprisingly (to me), in some tests, repeated local variable declarations run faster than once outside function ones. Maybe reading values stored in local variables takes less time, I don't know.
– user11132556
Mar 27 at 19:05
@user11132556 I supposed you have not tested that in a release build with optimisation enabled and stuff? It's almost useless to test debug builds.
– Guillaume Racicot
Mar 27 at 20:49
@user11132556 I supposed you have not tested that in a release build with optimisation enabled and stuff? It's almost useless to test debug builds.
– Guillaume Racicot
Mar 27 at 20:49
add a comment |
2 Answers
2
active
oldest
votes
you look at a wrong question/problem
- if you always want a given method returns a given value whatever the instance and the historic of course do not use an attribute for that
- if the value of the attribute depends on the instance use a (non static) attribute
- if the value does not depend on the instance but can change during the execution use a class instance (e.g. static)
But I do not want them to be visible to other functions of the same class.
This is not possible just using the visibility, all the methods of the class have (fortunately) access to the attributes of the class, this cannot be a problem because the class knows what it does. Of course the attribute must be at least protected.
Anyway
- if the value do not depends on the instance you can may be use a static var defined in the method.
- else you can use a map defined static in the method where the key are the instances and the value the expected value, but that complicate the way to do because you at least need to take into account the deletion of instances. I encourage you to not do that kind of gaz factory
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
1
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
add a comment |
It is not time-consuming to allocate local variables, they are part of a call stack that is allocated in large blocks. What you are intending to do is the opposite of what is good practice. Good practice is to declare a variable in the smallest scope you can.
Also, you cannot hide fields from member functions in the same class.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383428%2fclass-variable-with-scope-restricted-to-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you look at a wrong question/problem
- if you always want a given method returns a given value whatever the instance and the historic of course do not use an attribute for that
- if the value of the attribute depends on the instance use a (non static) attribute
- if the value does not depend on the instance but can change during the execution use a class instance (e.g. static)
But I do not want them to be visible to other functions of the same class.
This is not possible just using the visibility, all the methods of the class have (fortunately) access to the attributes of the class, this cannot be a problem because the class knows what it does. Of course the attribute must be at least protected.
Anyway
- if the value do not depends on the instance you can may be use a static var defined in the method.
- else you can use a map defined static in the method where the key are the instances and the value the expected value, but that complicate the way to do because you at least need to take into account the deletion of instances. I encourage you to not do that kind of gaz factory
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
1
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
add a comment |
you look at a wrong question/problem
- if you always want a given method returns a given value whatever the instance and the historic of course do not use an attribute for that
- if the value of the attribute depends on the instance use a (non static) attribute
- if the value does not depend on the instance but can change during the execution use a class instance (e.g. static)
But I do not want them to be visible to other functions of the same class.
This is not possible just using the visibility, all the methods of the class have (fortunately) access to the attributes of the class, this cannot be a problem because the class knows what it does. Of course the attribute must be at least protected.
Anyway
- if the value do not depends on the instance you can may be use a static var defined in the method.
- else you can use a map defined static in the method where the key are the instances and the value the expected value, but that complicate the way to do because you at least need to take into account the deletion of instances. I encourage you to not do that kind of gaz factory
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
1
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
add a comment |
you look at a wrong question/problem
- if you always want a given method returns a given value whatever the instance and the historic of course do not use an attribute for that
- if the value of the attribute depends on the instance use a (non static) attribute
- if the value does not depend on the instance but can change during the execution use a class instance (e.g. static)
But I do not want them to be visible to other functions of the same class.
This is not possible just using the visibility, all the methods of the class have (fortunately) access to the attributes of the class, this cannot be a problem because the class knows what it does. Of course the attribute must be at least protected.
Anyway
- if the value do not depends on the instance you can may be use a static var defined in the method.
- else you can use a map defined static in the method where the key are the instances and the value the expected value, but that complicate the way to do because you at least need to take into account the deletion of instances. I encourage you to not do that kind of gaz factory
you look at a wrong question/problem
- if you always want a given method returns a given value whatever the instance and the historic of course do not use an attribute for that
- if the value of the attribute depends on the instance use a (non static) attribute
- if the value does not depend on the instance but can change during the execution use a class instance (e.g. static)
But I do not want them to be visible to other functions of the same class.
This is not possible just using the visibility, all the methods of the class have (fortunately) access to the attributes of the class, this cannot be a problem because the class knows what it does. Of course the attribute must be at least protected.
Anyway
- if the value do not depends on the instance you can may be use a static var defined in the method.
- else you can use a map defined static in the method where the key are the instances and the value the expected value, but that complicate the way to do because you at least need to take into account the deletion of instances. I encourage you to not do that kind of gaz factory
edited Mar 27 at 17:54
answered Mar 27 at 17:48
brunobruno
19.3k3 gold badges16 silver badges28 bronze badges
19.3k3 gold badges16 silver badges28 bronze badges
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
1
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
add a comment |
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
1
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
"this is not possible" Well, introducing inheritance level indirections maybe.
– πάντα ῥεῖ
Mar 27 at 17:49
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
@πάνταῥεῖ yes I said badly, I edited my answer
– bruno
Mar 27 at 17:51
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
"because the class knows what it does" At least its developer/maintainer should do ;-D ...
– πάντα ῥεῖ
Mar 27 at 17:53
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
@πάνταῥεῖ the existence of the visibility says the developers don't know what they do (joke) ^^
– bruno
Mar 27 at 17:55
1
1
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
"I encourage you to not do that kind of gas factory" You made my day now, that deserves an upvote :-D :-D :-D
– πάντα ῥεῖ
Mar 27 at 17:56
add a comment |
It is not time-consuming to allocate local variables, they are part of a call stack that is allocated in large blocks. What you are intending to do is the opposite of what is good practice. Good practice is to declare a variable in the smallest scope you can.
Also, you cannot hide fields from member functions in the same class.
add a comment |
It is not time-consuming to allocate local variables, they are part of a call stack that is allocated in large blocks. What you are intending to do is the opposite of what is good practice. Good practice is to declare a variable in the smallest scope you can.
Also, you cannot hide fields from member functions in the same class.
add a comment |
It is not time-consuming to allocate local variables, they are part of a call stack that is allocated in large blocks. What you are intending to do is the opposite of what is good practice. Good practice is to declare a variable in the smallest scope you can.
Also, you cannot hide fields from member functions in the same class.
It is not time-consuming to allocate local variables, they are part of a call stack that is allocated in large blocks. What you are intending to do is the opposite of what is good practice. Good practice is to declare a variable in the smallest scope you can.
Also, you cannot hide fields from member functions in the same class.
answered Mar 27 at 17:43
Sean FSean F
2,1866 silver badges14 bronze badges
2,1866 silver badges14 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383428%2fclass-variable-with-scope-restricted-to-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
" I suppose it is time consuming to allocate memory to local variables every time." Is this actually true? Have you tested that?
– Jodocus
Mar 27 at 17:41
Sounds like a XY-problem for me. What's the reason for this requirement: "But I do not want them to be visible to other functions of the same class."? However such could be probably achieved with inheritance constructs.
– πάντα ῥεῖ
Mar 27 at 17:47
@user11132556 " I suppose it is time consuming to allocate memory to local variables every time." It's usually not. Please don't do premature that kindo micro optimization, before you actually hit a performance bottleneck with your application when local variables are allocated. If so it might be rather a problem with those constructor functions, but not really the allocation of the variable representation itself.
– πάντα ῥεῖ
Mar 27 at 18:01
Good question. I had not tested, but now I did. In fact, it really takes time to allocate memory to local variables. But the subsequent function operations probably will take much more time. Surprisingly (to me), in some tests, repeated local variable declarations run faster than once outside function ones. Maybe reading values stored in local variables takes less time, I don't know.
– user11132556
Mar 27 at 19:05
@user11132556 I supposed you have not tested that in a release build with optimisation enabled and stuff? It's almost useless to test debug builds.
– Guillaume Racicot
Mar 27 at 20:49