In Example 20.10, identify the scope/storage class of each object's creation/definition. If there is a name clash, describe the error.
[ fromfile: scopestorage.xml id: ex-scopestorage ]
Example 20.10. src/storage/storage.cpp
#include <QString> int i; static int j; extern int k; const int l=10; extern const int m=20; class Point { public: QString name; QString toString() const; private: static int count; int x, y; }; int Point::count = 0; QString Point::toString() const { return QString("(%1,%2)").arg(x).arg(y); /* Scope: ______________ Storage class: ____________ */ } int main(int argc, char** argv) { int j; register int d; int* ip = 0; ip = new int(4); Point p; Point* p2 = new Point(); }
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
S/SC of argc and argv: _________________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ | |
Scope: ______________ Storage class: ____________ |
<include src="src/storage/storage.cpp" href="src/storage/storage.cpp" id="storagecpp" mode="cpp"/>
Example 20.11. src/storage/storage-solution.cpp
#include <qstd.h> using namespace qstd; int i; static int j; extern int k; const int l=10; extern const int m=20; class Point { public: QString name; QString toString() const; private: static int count; int x, y; }; int Point::count = 0; QString Point::toString() const { return QString("(%1,%2)").arg(x).arg(y); } int main(int argc, char** argv) { int j; register int d; int* ip = 0; ip = new int(4); Point p; Point* p2 = new Point(); }
Global / Static | |
File / Static | |
Global / Static | |
This is a file scope variable, but it might not be stored anywhere, since it is easily optimized out by the compiler. | |
Global / static | |
Global scope, but no storage class, since it is not an object | |
class / various - all class scope variables have various storage class, since it depends on how the containing objects were created. | |
class / various | |
Point::count has global scope, but count by itself has class scope inside class Point. The object has static storage class. | |
class / various | |
block / stack | |
block / stack | |
block / stack | |
no scope / heap - the object created has no identifier, so it has no scope. However, it is in the heap storage class. | |
block / stack | |
no scope / heap |
<include src="src/storage/storage-solution.cpp" href="src/storage/storage-solution.cpp" role="solution" mode="cpp" condition="solution"/>
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |