20.3.2. Exercises: Storage Class

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;                              1
static int j;                       2
extern int k;                       3
const int l=10;                     4
extern const int m=20;              5

class Point                         6
{
    public:
    QString name;                   7
    QString toString() const;
    private:
    static int count;
    int x, y;                       8
};

int Point::count = 0;               9

QString Point::toString() const {
    return QString("(%1,%2)").arg(x).arg(y); 
                                    /* Scope: ______________   Storage class: ____________ */
}

int main(int argc, char** argv)     10
{
    int j;                          11
    register int d;
    int* ip = 0;                    12
    ip = new int(4);                13
    Point p;                        14
    Point* p2 = new Point();        15
}


1

Scope: ______________ Storage class: ____________

2

Scope: ______________ Storage class: ____________

3

Scope: ______________ Storage class: ____________

4

Scope: ______________ Storage class: ____________

5

Scope: ______________ Storage class: ____________

6

Scope: ______________ Storage class: ____________

7

Scope: ______________ Storage class: ____________

8

Scope: ______________ Storage class: ____________

9

Scope: ______________ Storage class: ____________

10

S/SC of argc and argv: _________________

11

Scope: ______________ Storage class: ____________

12

Scope: ______________ Storage class: ____________

13

Scope: ______________ Storage class: ____________

14

Scope: ______________ Storage class: ____________

15

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;   1
static int j; 2
extern int k; 3 
const int l=10;  4
extern const int m=20;  5

class Point 6
{
    public:
    QString name; 7
    QString toString() const;    
    private:
    static int count;
    int x, y;  8
};

int Point::count = 0; 9

QString Point::toString() const {
    return QString("(%1,%2)").arg(x).arg(y);  10
}

int main(int argc, char** argv) 11
{
    int j;  12
    register int d;
    int* ip = 0;  13
    ip = new int(4);  14
    Point p; 15
    Point* p2 = new Point(); 16
}


1

Global / Static

2

File / Static

3

Global / Static

4

This is a file scope variable, but it might not be stored anywhere, since it is easily optimized out by the compiler.

5

Global / static

6

Global scope, but no storage class, since it is not an object

7

class / various - all class scope variables have various storage class, since it depends on how the containing objects were created.

8

class / various

9

Point::count has global scope, but count by itself has class scope inside class Point. The object has static storage class.

10

class / various

11

block / stack

12

block / stack

13

block / stack

14

no scope / heap - the object created has no identifier, so it has no scope. However, it is in the heap storage class.

15

block / stack

16

no scope / heap

<include src="src/storage/storage-solution.cpp" href="src/storage/storage-solution.cpp" role="solution" mode="cpp" condition="solution"/>