• Advertisement

Archived

This topic is now archived and is closed to further replies.

Quick C++ question

This topic is 5909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what is the keyword auto used for? I haven''t been coding long, but I don''t think I''ve ever see it in use.

Share this post


Link to post
Share on other sites
Advertisement
Basically, auto means that the variable is going to be local; in other words, it will be created within its scope and destroyed when its scope goes away.

If you had:
  
float justDoIt()
{
auto float x, y, z;
...
return x*y*z;
}


Then the variables x, y, and z are created in the function and destroyed when the function returns.

They exist only in the body of the function in which the definition appears.

This type of automatic storage is a way to conserve memory since they are destroyed when they go out of scope.

The reason why you''ll probably never see this in anyone''s code is because it goes before all you variable declerations in a function as a default. I have rarely seen people use this keyword.

Good luck!

Share this post


Link to post
Share on other sites

  • Advertisement