Reputation: 517
Are storage classes in C++ (auto, register, static, extern and mutable) classes with the meaning of 'blueprints for objects' or is the name 'classes' somewhat misleading in this context?
Upvotes: 1
Views: 307
Reputation: 11
Microsoft Developer Network defines storage class as, "A storage class in the context of C++ variable declarations is a type specifier that governs the lifetime, linkage, and memory location of objects."
Lifetime refers to how long the variable "hangs around" in memory from the point at which it is declared and the point at which it is destroyed (the memory it used is released). For the most part, once a variable goes out of scope, its memory will be released back to the operating system for reuse.
Linkage refers to the visibility of a variable outside of the file that contains it.
Memory location refers to the place in which the variable is found in memory. This doesn't refer to the physical memory address as you might expect but more to the logical division of memory that applies to a running application. There are two logical memory areas known as the stack and the heap. The stack is a location in memory where intrinsic data is stored as well as memory addresses (pointers). It operates in the form of data structure known as a stack. Like a cafeteria stack of plates, items are pushed on top of the stack and other items are pushed further down. To remove an item from the stack, it is popped off, used, and discarded.
The heap, or free store, is a pool of memory that is used to store objects that dynamically allocated at run time by your application. An object is what you will learn about in the next topic on object-oriented programming. You create and destroy objects on the heap by using specific instructions in your program code.
From the course introduction to C++ on edx.org
Upvotes: 0
Reputation: 385144
Whether the term "class" as in "storage class" is misleading is open for debate, but we can all certainly agree that it is a completely different meaning to "class" as in "class type" as in class
, yes.
As Hans suggests, "storage category" may be clearer in the modern era.
To make a "blueprint for object [type]", you can define a class template.
Upvotes: 2
Reputation: 141554
"Storage class" is nothing to do with "class".
IMHO the Standard could do with a little bit of tidying up in this respect, it would make more sense to only say storage class specifier, meaning those keywords.
For example, a variable can be declared with the extern
storage class specifier. But it does not make sense to then describe that variable as "having extern storage class". Instead the proper terminology for such a variable is static storage duration and external linkage. Variables can also be defined with those two properties without using the extern
keyword, and it makes no difference whether or not there was also an extern
declaration.
In a few places the standard uses expressions like "variable having register storage class", this actually means "variable having been declared with the register
storage class specifier".
Upvotes: 0