261
Файл взят с сайта - http://www.natahaus.ru/ где есть ещё множество интересных и редких книг, программ и прочих вещей. Данный файл представлен исключительно в ознакомительных целях. Уважаемый читатель! Если вы скопируете его, Вы должны незамедлительно удалить его сразу после ознакомления с содержанием. Копируя и сохраняя его Вы принимаете на себя всю ответственность, согласно действующему международному законодательству . Все авторские права на данный файл сохраняются за правообладателем. Любое коммерческое и иное использование кроме предварительного ознакомления запрещено. Публикация данного документа не преследует за собой никакой коммерческой выгоды. Но такие документы способствуют быстрейшему профессиональному и духовному росту читателей и являются рекламой бумажных изданий таких документов. Все авторские права сохраняются за правообладателем. Если Вы являетесь автором данного документа и хотите дополнить его или изменить, уточнить реквизиты автора или опубликовать другие документы, пожалуйста, свяжитесь с нами по e-mail - мы будем рады услышать ваши пожелания.

C++

  • Upload
    miqamk

  • View
    6

  • Download
    4

Embed Size (px)

DESCRIPTION

for Real Programmers

Citation preview

  • - http://www.natahaus.ru/ , . . ! , . , . . . . . . , , , e-mail - .

  • CCCC++++++++

  • ......................................................................................................................................... 9 ..................................................................................................................... 9

    1. ............................................................................................ 11

    1. ++? .................................................................... 13 ++......................................................................................................................................................... 13 ++................................................................................................................................ 15 ................................................................................................................................ 16 ...................................................................................... 17

    2. ++ ...................................................................................................................... 19 ........................................................................................................................ 19

    const............................................................................................................................................................ 19 ....................................................................................................... 23

    .................................................................................................................. 25 .................................................................................................................................... 25 ................................................................................................................................................. 28 ................................................................................................................................................. 29

    ...................................................................................................................................... 33 .......................................................................................................................................... 33 ............................................................................................................................................ 40 ............................................................................................................................................ 41 ............................................................................................................................ 46

    3. ............................................................................ 55 ? ............................................................................................. 55 ................................................................................................................................................... 55 .................................................................................................................................... 56 ........................................................................................ 56

    ................................................................................................................................ 57 ...................................................................................................................... 57 ................................................................................................................. 57 ................................................................................................... 58 ................................................................................................................................ 58 .................................................................................................. 59 !...................................................................................... 59 ............................................................................................................................................. 59

    ......................................................................... 59 .............................................................................. 60 ................................................................................ 60 ............................................................................................. 60

    4. .......................................................................................................................... 63

  • 4

    ANSI ...........................................................................................63 ..................................................................................................63 ..........................................................................................................66 ...............................................................................................................67

    ...............................................................................................69 .............................................................................................................................69

    2. ..................................................................................................71

    5. ...............................................................................................................73 ......................................................................................................................................73 .................................................................................................................75 -> .............................................................................................................................................75 ...................................................................................................75 ....................................................................................................................76 .............................................................................................77 ?.....................................................................................................78

    .................................................................................................................................................78 NULL ...............................................................................................................78 ...........................................................................................................................80 ..............................................................................................................................................82

    6. .....................................................................85 ............................................................................................................85 ......................................................................................................................................86 ............................................................................................................................................87 ...............................................................................................................................................87 .............................................................................................................................................88 ...............................................................................................89

    C++.....................................................................................................................................90 ? ...................................................................................................................................90 ....................................................................................................................................90 .................................................................................................................92 /..................................................................................................................92

    7. .........................................................................93 .........................................................................................................................93 .......................................................................................................................93 ........................................................................................................94 ............................................................................................................................96

    .............................................................................................................................................................96 ....................................................................................97 .................................................................................................................................................98 .........................................................................................................................99 ...................................................................................................102 ......................................................................................................................................103 ...............................................................................................................103 ....................................................................................................................105

    .....................................................................................................................................106 ...................................................................................................106 .......................................................107 ................................................107

    ................................................................................................................................................107 ....................................................................................................................................................108

    8. , .......................................................................111 [] ..........................................................................................................................111

  • 5

    ........................................................................................................... 111 [] ............................................................................................... 112 ........................................................................................................ 112 [] ........................................................................................ 113 [].................................................................................................................... 113

    ...................................................................................................................................................... 114 ................................................................................................. 114 ......................................................................................................... 115 ->........................................................................................ 116 - ................................................................................................................................ 117

    .................................................................................................................................................. 117 ............................................................................................................................ 118 .......................................................................................................................... 118 ? ............................................................................................................................................. 119 , ............................................................................................ 119 ................................................................................................................................... 120 ....................................................................................................... 121

    ............................................................................................................................. 123 ....................................................................................... 124 ...................................................................................................................... 126 ...................................................................................................... 127 ................................................................................................................................ 129 ..................................................................................................................................................... 131

    9. .............................................................. 137 ........................................................................................................................ 137 ............................................................................................................................................... 137 .................................................................................................................................................... 138 ? .................................................................................................................................................. 138

    ................................................................................................................................. 138 .................................................................................................................. 139 ......................................................................................................................................... 140 ...................................................................................................... 141 ................................................................................................................................. 144 ......................................................................................................................... 145

    .............................................................................................................................. 145 ....................................................................................................................... 146 ConstPtr......................................................................................................................................... 147 LockPtr .......................................................................................................................................... 149 ..................................................................................................... 150 ............................................................................................................ 151 .................................................................................................................................................... 152

    ................................................................................................................................................... 152 .......................................................................................................................... 152 ............................................................................................................ 153 ........................................................................................................................ 154 ............................................................................................................................. 154

    ................................................................................................................ 155

    3. ..................................................................................................................... 157

    10. ....................................................................................... 159 ......................................................................................................... 159 ......................................................................................... 160 ...................................................................................................................... 160 ...................................................................................................... 161

    ..................................................................................................................... 162

  • 6

    ....................................................................................................................................163 ........................................................................................................164 .........................................................................................................165 ................................................................................................166

    ...........................................................................................................................................167 11. ............................................169 ..........................................................................................................................169

    make- ..........................................................................................................................................170 make- ....................................................................170 .........................................................................170 ......................................................................171 ......................................................................................................................172 : ............................................................172 =!....................................................................................173

    ......................................................................................................................................173 ..............................................................................................................................174 ...............................................................................175 ............................................................................................................176

    ..........................................................................................................................................177 12. ................................................................................................179 ..............................................................................................................................179 ...........................................................................180 ..........................................................................................................................180 ..............................................................................................................................181 .............................................................................................................................181 .................................................................................................................................183

    .....................................................................................................................184 ..................................................................................................................185 ....................................................................................................187 .......................................................................................................189

    ...................................................................................................189 ............................................................................................................................................189 ...............................................................................................189 ...................................................................................189

    4. ....................................................................................................191

    13. .........................................193 NEW DELETE...................................................................................................193 ......................................................................................................193 new delete.................................................................................................196 new....................................................................................................................197 ....................................................................................................197 ..........................................................................................................198

    ?...................................................................................................199 ............................................................................................................................199 .....................................................................................200 ...............................................................................200 .........................................................................................200 .................................................................200 ...........................................................................................................................................204

    14. ..............................................................................205 ................................................................................................................................205 ........................................................................................................205

  • 7

    ............................................................................................................................ 208 ....................................................................................................................... 208

    ........................................................................................................................................ 210 ...................................................................................................... 210 .............................................................................................................. 211 ............................................................................................. 211 ....................................................................................................... 212 ................................................................................................................. 213 .................................................................................................. 213

    ............................................................................................................................. 214 ................................................................................................................................. 214 ................................................................................................................................. 215 ........................................................................................................ 215 ............................................................................................................. 215 .................................................................................................................. 216

    15. ..................................................................................................... 217 .................................................................................................................................... 217 , ? ...................................................................................................... 217 ................................................................................................................................... 220

    , .................................................................................................. 223 .............................................................................................................. 223 ................................................................................................................................. 223 .................................................................................................................................................. 227 ........................................................................................................ 229

    .................................................................................................................................... 229 ....................................................................................................................... 229 ............................................................................................................ 232 ................................................................................................................................... 233 : C++ ....................................................................................... 234

    .............................................................................................................................. 236 VoidPtr ............................................................................................................................ 236 ........................................................................................................................ 237 ............................................................................................................ 238 ............................................................................................................................ 238 ........................................................................................................................................... 239 ............................................................................................. 239

    ............................................................................................................................................. 239 16. ................................................................................................................. 241 .............................................................................................................................................. 241 ................................................................................................................................................ 241 ................................................................................................................................. 242 ................................................................................................................................. 243 ....................................................................................................................... 244

    ................................................................................................ 245 ............................................................................................................... 245 ............................................................................................................. 245 .......................................................................................................... 246 ............................................................................................................................... 248 ........................................................................................................................................... 251 ................................................................................................................................... 251 ........................................................................................................... 251 ................................................................................................ 251 ? .................................................................................................... 252

    ..................................................................................... 252 .................................................................................................... 252 .............................................................................................................................. 253

  • 8

    ....................................................................................................................................254 ...................................................................................................................254 .........................................................................................................................................255 .........................................................................................................255

    ...........................................................................................................................255 . JAVA C++ .....................................................................................................257

  • , , , . , , . , ego . , . , - , , .

    , , , AP Professional. , , . , , !

    (John Trudeau) Apple Computer, ++. , , , , , .

    ++ - . (Neal Goldstein), (Larry Rosenstein), (Eric Berdahl), (John Brugge), (Dave Simmons) (Dave Buell). , , .

    Microsoft Corporation, , 90 90 . Microsoft, , , , . , Microsoft, Microsoft , .

    (James Coplien), , Advanced C++ Programming Styles and Idioms . ++. , .

    , (Bjarne Stroustrup) , . , SmallTalk , . ++ , , . , , . ++ - .

    , . , , ( , IEEE Computer) ,

  • 10

    ++. , , . , , .

    . -, - , , , . ++ , , , , . , , . , .

    , , , . ++, - : ( ), ( ), ( ) .. , (>). , , ? , .

    , , . , , , , . : , . , .

    1998 .

  • : ++? . , , ++.

    1

  • ++?

    , 2 768 942 ++, , , . . ++ , . , . , 2 768 940 , ++. , , . - , . ++ .

    . , , ++. , ++ - . , ; , , , , ( ). , , - . Annotated C++ Reference Manual, ARM .

    , . ++: . ++ , . ++ , , , . . , , , . , ++ , . . , , , , .

    ++ ++ , . , , . -, ++ - . ! . ++ , (, , ) //. , , (, ) .

    1

  • 14

    ! . ++ , - . Smalltalk, , , ? ++ Cobol 90-, , . ++ , . , ++ . , ?

    ! , . , ? . ++ , . ( ++ ), . - , , . , Smalltalk Lisp, , - . ++ . , .

    . ++ : , . ++ . . (, i++). , , :

    cout

  • 15

    ++, , : , . .

    ++ . . . , . , . , , . ; , . . , .

    ++ C++ , , . - ? , , - . , . C++. , , . . C++, :

    .

    .

    .

    C++, . , , .

    C++. , C++ , . , / cout cin. 1 , C++ . 1 , , C++.

    (indirection) , : , , , . . , (delegation), - . , C++ , , , . (pointer); . C++ . , , , ; ; ; , , . , , , .

    , . - , C++

  • 16

    - . , (homomorphic derivation) C++. -. , , . C++ .

    (memory space) , . C++ new delete, , . , , , . - , , Microsoft, Apple Taligent. , , . , C++. , , .

    . . , C++ , , , , , .

    . , , , , . . - C++.

    C++ , ; . , , . 2 . 3 , . 4 ANSI , .

    2 . , , .

    3 C++. . , . , . , , .

    4 C++ . , , C++, . , C++, , , - .

  • 17

    : . ? , , , . , , , . . , . , , - , .

    , , . , . , .

    (inline) , . :

    class Foo {

    public:

    void MemberFn();

    }; inline void Foo::MemberFn()

    {

    ...

    }

    : class Foo {

    public:

    void MemberFnO {...};

    };

    , , . . , , . .

    , , !

  • ++

    C++ , C++ ( ) . , , C++ . , - . Annotated Reference Manual . , . C++!

    , , . : .

    const const, , C++. , - , .

    const, , . , . .

    const int j = 17; //

    j = 29; // ,

    const int i; // ,

    , , , . , , . i , . int , const int j=17; , int j(17).

    , . , . , , .

    const i = 17;

    int& j = 1; // , j

    2

  • 20

    , , j. , , . , , , .

    const #define :

    const int i = 17;

    #define i 17;

    , , . , - , . , . .

    () : . C++ , .

    class Foo {

    public:

    enum Status { kOpen = 1, kClosed };

    };

    // -

    Foo::Status s = Foo::kOpen;

    , . , kOpen kClosed . , . (, ).

    , : . ; , .

    const int* p;

    int i = 17;

    p = &i; //

    *p = 29; //

    . class foo {

    public:

    int x;

    };

    const foo* f = new foo;

    f->x = 17; // ,

  • 21

    : , .

    int i = 17;

    int j = 29;

    int* const p; // !

    int* const p1 = &i; //

    *p1 = 29; // ; , ,

    //

    p1 = &j; //

    (- !) . .

    int i = 17;

    int j = 29;

    const int* const p; // .

    const int* const p1 = &i; //

    *p1 = 29; //

    p1 = &j; //

    , .

    void f(const int* p)

    {

    *p = 17; //

    int i = 29;

    p = &i; // , ?

    }

    // -

    int i = 17;

    f(&i); // ,

    , , . -. , .

    void f(const int& p)

    {

    p = 17; //

    int i = 29;

    p = i; // ( )

    }

    // -

    int i = 17;

    f(i); //

  • 22

    , , , .

    void f(int*);

    int i = 17;

    const int* p = &i;

    const int j = 29;

    f(&i); // , i

    f(p); //

    f(&j); // , j -

    , . f , .

    this . , this , . const ; , , .

    class foo {

    private:

    int x;

    public:

    void f() const;

    void g();

    };

    void h(int*);

    void m(foo*);

    void foo::f();

    {

    x = 17; // :

    this->g(); // : g

    h(&x); // : h x

    m(this); // : m()

    }

    this. foo this const foo* this;. . , g - foo, ; , . this. .

    C++ const, . , this, . , , , , . ,

  • 23

    . : (), .

    , C++ C. C++ , , . , .

    int x = 17;

    C++ int. ... int. . int , . , . , , .

    C++, .

    , .

    {

    int i;

    foo f(constructor_args);

    // i f

    }

    . . , , , , . :

    {

    int i;

    foo f;

    SomeFunction(&f);

    }

    SomeFunction , . SomeFunction - , f. SomeFunction , , - SomeFunction, !!! .

    (heap), newnewnewnew.

    foo* f = new foo(constructor_args);

    . new . ? , :

  • 24

    - delete . ; , .

    . , . , (dereferencing) . , .

    {

    foo f;

    foo* p = &f;

    f.MemberFn(); //

    p->MemberFn(); //

    p = new foo;

    foo& r = *p; //

    r.MemberFn(); // , p->MemberFn()

    }

    , . -> . , (. ->) (member selectors). , , , - .

    delete , . ( ) , , , , , . , C++ new . . delete , . new , . delete , .

    , , :

    1. .

    2. delete.

    , : .

    , , (, ). new ( , ) , , . . , new, , .

  • 25

    , , . Foo PFoo. Foo; . , . , operator Foo*() PFoo , Foo* , g().

    class PFoo {

    private:

    Foo* f;

    public:

    PFoo() : f(new Foo) {}

    ~PFoo() { delete f; }

    operator Foo*() { return f; }

    }

    void g(Foo*);

    {

    PFoo p;

    g(p); // operator Foo*()

    // p, Foo

    }

    , , , operator Foo*(), PFoo. .

    . , - , . , , .

    ANSI- (exception). , . , . , , .

    C++ . , , . , C++ . , , .

    :

    ;

    ;

    ;

    ;

    .

  • 26

    , , . . , .

    C++ , . , .

    class Foo {

    public:

    static int y; //

    static void GFn(); //

    int x; //

    Foo(); //

    void Fn(); //

    typedef int (*IntFn)(); //

    enum Status { kOpen = 0, kClosed }; //

    struct Bar { //

    int a;

    int b;

    static void BarFn();

    }

    private:

    void Hn();

    };

    . , a GFn() , Foo. Foo , :::

    Foo::Foo()

    {

    GFn(); // Foo

    }

    void f()

    {

    Foo::GFn(); //

    }

    , IntFn, Status Bar Foo, . , r:

    Foo::Bar b;

    Foo::Bar::BarFn();

    , .

    , Foo Fn(), (instance) . - , . ->. (, , C++) ,

  • 27

    . , , :

    Foo f;

    f.Gfn(); // , Foo::GFn();

    C++ . , . , ; public, private, protected . , : (public), C++.

    , . ? :

    .

    -, .

    -, .

    C++ . , , C++ ( ). (- ), v-, .

    C++ . . , , :

    ( ).

    -.

    .

    , , , .

    , . (private) (public). , , , . , .

    , , .

    C++ , . .

  • 28

    , . . :

    // Foo.cpp

    typedef int Symbol;

    // Bar.cpp

    typedef void (*Symbol)();

    , . #include. Symbol , , -. , , . , .

    // Foo.cpp

    int Symbol;

    // Bar.cpp

    void (*Symbol)();

    , . , , . ; . , .

    // Foo.cpp

    static int Symbol;

    // Bar.cpp

    static void (*Symbol)();

    static, , ; .

    , , :: :

    ::Fn(); //

    int x = ::i; // x

    ::SomeType y; //

    , , .

    C++ . , . , , . .

    , , .

    void Fn();

  • 29 void Fn(int);

    void Fn(long); // , long int

    int Fn(int); //

    int Fn(char*); // ,

    void Fn(int, char*);

    void Fn(char*, int); // ,

    void Fn(char* s, int x, int y = 17); //

    Fn(hello, 17); //

    , . ( =17) , ( ).

    , , . , .

    class Foo {

    public:

    void Fn();

    void Fn() const; // !

    };

    Foo* f = new Foo;

    f->Fn(); //

    const Foo* f1 = f;

    f1->Fn(); //

    C++ ( , ) , , , . , . , (visibility) C++.

    ; . , .

    , , .

    class Mixin {

    private:

    int x;

    protected:

    int y;

    public:

    Mixin();

    Void a();

    };

  • 30 class Foo : private Mixin {...};

    class Bar : public Foo {...};

    Mixin (). Foo, Mixin::A(). Mixin , Foo ( r ). Foo (), Bar .

    , .

    class Foo {

    protected:

    virtual void Fn();

    };

    class Bar : public Foo {

    public:

    virtual void Fn();

    };

    Foo Fn() , . . .

    class Foo {

    private:

    int x;

    public:

    void Fn();

    };

    class Bar : public Foo {

    private:

    int x; //

    public:

    void Fn(); //

    };

    //

    Bar *b = new Bar;

    b->Fn(); // Bar::Fn()

    Foo* f = b; // , Foo

    f->Fn(); // Foo::Fn()

    . Foo Foo::. Bar r::. , , . Fn() , . C++ .

    , . , , . . , virtual .

  • 31

    , , . DWIMNIS: Do what I mean, not what I say ( , , , ). , C++ , ( ) . Bar::Fn():

    class Foo {

    public:

    virtual void Fn();

    };

    class Bar {

    public:

    void Fn(); //

    };

    Bar* b = new Bar;

    b->Fn(); // Bar::Fn()

    Foo* f = b;

    f->Fn(); // Bar::Fn()

    . -, , Foo::Fn(). -, , Fn() , . .

    , , , . ? ? , .

    class Foo {

    public:

    virtual void Fn();

    virtual void Fn(char*);

    };

    class Bar {

    public:

    virtual void Fn(int); // ,

    };

    , - , . :

    Fn() r* , void Fn(int). Bar*.

    Bar* Foo* , Foo, void Fn(int). , , Bar::Fn() . , virtual .

    - , , , , - . , . . , : , , .

  • 32

    - (friend). , , , . , . ; - , .

    class Foo;

    class BarBar {

    public:

    int Fn(Foo*);

    };

    class Foo {

    friend void GlobalFn(); //

    friend class Bar; //

    friend int BarBar::Fn(Foo*); //

    friend class DoesNotExist; // .

    private:

    int x;

    struct ListNode {

    ListNode* next;

    void* datum;

    ListNode() : next(NULL), datum(NULL) {}

    } head;

    protected:

    int y;

    public:

    void G();

    };

    void GlobalFn()

    {

    Foo* f = new Foo;

    f->x = 17; // -

    }

    class Bar {

    private:

    Foo* f;

    public:

    Bar() : f(new Foo) {}

    void WalkList();

    };

    void Bar::WalkList()

    {

    Foo::ListNode* n = f->head.next;

    for (; n != NULL; n = n->next)

    cout datum x; }

  • 33

    , public, protected private. , ; , . Foo . Foo, . , - DoesNotExist. , , . Foo. , . , .

    , , , .

    , , , , . , (, ).

    , C++ , .

    , , .

    class Foo {

    public:

    Foo();

    };

    class Bar : public Foo { // 1.

    public:

    Bar();

    };

    class BarBar {

    private:

    Foo f; // 2.

    };

    Foo f; // 3. Foo

    Foo* f1 = new Foo; // 3. ,

    (. ) Bar - Foo, Foo . , f BarBar, Foo . , Foo .

    , , . , . , . -

  • 34

    , , , , .

    class Foo {

    public:

    Foo(char*);

    };

    Foo f; // !

    class Bar : public Foo {

    public:

    Bar();

    };

    Bar::Bar()

    {

    // ! Foo

    }

    , C++ : . , .

    class Foo {

    public:

    Foo(char*);

    };

    class Bar : public Foo {

    public:

    Bar(char*);

    };

    class BarBar {

    private:

    Foo f;

    int x;

    public:

    BarBar();

    };

    Bar::Bar(char* s) : Foo(s) {...}

    BarBar::BarBar : f(Hello), x(17) {...}

    Bar Foo. , . Bar , , Foo. BarBar ( ) f . ( ):

    BarBar::BarBar() : f(Hello) { x = 17; }

  • 35

    0 ( C++) int , 17. - . , , , .

    , (, , ). , , .

    , , . . , , :

    1. ( , : , ).

    2. .

    3. , .

    , ... . . , . , . , , .

    (copy constructor) :

    class Foo {

    public:

    Foo(const Foo&);

    };

    Foo::Foo(const Foo& f)...

    . .

    void Fn(Foo f) {...}

    void Gn(Foo& f) {...}

    Foo f;

    Foo f1(f);

    Foo f2 = f; // , !

    Fn(f); //

    const Foo f3;

    Gn(f3); //

    //

    . Foo f1(f); Foo, Foo . ,

  • 36

    Foo . , ; , . , Foo ; , .

    Foo f2 = f - =, . , , : ? , . , , .

    Fn() Foo. , Fn(). , .

    Gn(), , , C++ - :

    , ! , . , , , . Gn() , .

    .

    1. , .

    2. , .

    , ... . . , ? , .

    , , . , , , . .

    class Foo {...};

    class Bar : public Foo {

    private:

    Foo f;

    public:

    Bar(const Bar&);

    };

    // ,

    Bar::Bar(const Bar& b)

    {

    // !

    //

    //

    }

    // ,

    Bar::Bar(const Bar& b) : Foo(b), f(b.f) {...}

  • 37

    , . Foo . 99 100 , ; , . , . , ( b Foo Foo(b)).

    . , .

    class Serialized {

    private:

    static int NextSerialNumber;

    int serialNumber;

    public:

    Serialized(const Serialized&);

    Serialized();

    int SerialNumber();

    };

    // Serialized.cpp

    int Serialized::NextSerialNumber = 0;

    Serialized::Serialized() : serialNumber(NextSerialNumber++)

    {

    }

    Serialized::Serialized(const Serialized&) : serialNumber(NextSerialNumber++)

    {

    }

    int Serialized::SerialNumber()

    {

    return serialNumber;

    }

    ac , , , .

    , . ; new ( , ). , , . , . - . , ( ) .

    class Foo {

    protected:

    Foo();

    };

  • 38 class Bar : public Foo {

    public:

    Foo* Fn();

    };

    Foo Bar::Fn()

    {

    return new Foo; //

    }

    , , Foo . , ! . Foo Bar Foo. , .

    (anonymous instance) , ... , .

    struct Point {

    int X;

    int Y;

    Point(int x, int y) : X(x), Y(y) {}

    };

    double distance(Point p)

    {

    return sqrt(double(p.X) * double(p.X) + double(p.Y) * double(p.Y));

    }

    double d = distance(Point(17, 29));

    distance() . . , .

    Point, .

    . C++, . , , . , , . , :

    1. - 0.

    2. , , .

    3. . , main() .

  • 39

    4. , ( .), . 3 , , .

    . . .

    // file1.cpp

    Foo foo;

    Foo* f = &foo;

    // file2.cpp

    extern Foo* f;

    Foo f1(*f); //

    , . F* f = &foo; , (-) . , , foo . , , file1.cpp file2.. file2.cpp , f 0 (NULL ), .

    , .. .h , .. 0 1 , .. 1 0 .

    // Library.h

    class Library {

    private:

    static int count;

    static void OpenLibrary();

    static void CloseLibrary();

    public:

    Library();

    ~Library();

    };

    static Library LibraryDummy;

    inline Library::Library()

    {

    if (count++ == 0)

    OpenLibrary();

    }

    inline Library::~Library()

    {

    if (--count == 0)

    CloseLibrary();

    }

    // Library.cpp

    int Library::count = 0; //

    int aGlobal;

  • 40 Foo* aGlobalFoo;

    void Library::OpenLibrary()

    {

    aGlobal = 17;

    aGlobalFoo = new Foo;

    }

    void Library::CloseLibrary()

    {

    aGlobal = 0;

    delete aGlobalFoo;

    aGlobalFoo = NULL;

    }

    . : .h ., - Library.cpp. , , . , LibraryDummy. ., Library.h, LibraryDummy . main() exit() . LibraryDummy. , OpenLibrary() CloseLibrary() .

    , iostream. , , , .

    , ( , ) delete. .

    , . , , , , , ..

    , . , , .

    main() exit().

    C++ . base* ( ), , .

    class Foo {

    public:

    ~Foo();

    };

  • 41 class Bar : public Foo {

    private:

    int* numbers;

    public:

    Bar() : numbers(new int[17]) {...}

    ~Bar();

    };

    Bar* b = new Bar;

    delete b; // Bar::~Bar()

    Foo* f = new Bar;

    delete f; // ! Foo::Foo()!

    f , numbers, , . , ; (, , void*) Bar: :~().

    , . ! . , , 20 , 220 ! C++ .

    : . , .

    , delete, , . , , , - .

    class Foo {

    public:

    ~Foo();

    };

    Foo* f = new Foo;

    f->Foo::~Foo();

    , C++.

    C++ . , .

    =.

    Foo f;

    Foo f1;

    f1 = f;

    . f f1 - , : ,

  • 42

    f, , f1. . Foo , C++ . = , . , , , =. , , , .

    class String {

    private:

    char* s;

    public:

    String(char*);

    ~String();

    void Dump(ostream& os);

    };

    String::String(char* str) : s(NULL)

    {

    if (str == NULL) { // NULL

    s = new char[1];

    *s = \0;

    }

    else {

    s = new char[strlen(str) + 1];

    strcpy(s, str);

    }

    }

    String::~String()

    {

    delete s;

    }

    void String::Dump(ostream& os)

    {

    os s. , , . , Goodbye, String* s2 = new String( "Goodbye");. ; s1 , s1. s2->s. s2->s . s2, . C++!

  • 43

    , . , , . = .

    , , , . = , = . , . = . =.

    Foo f;

    Foo f1 = f; // ; f1

    f1 = f; // : f1

    = , , . , , = . :

    1. . = = .

    2. . = = .

    . , . .

    = = . =, : & X::operator=(const X&).

    class String {

    private:

    char* s;

    public:

    String(char*);

    ~String();

    String(const String&); // ,

    String& operator=(const String&);

    void Dump(ostream& os);

    };

    String::String(char* s) : s(NULL)

    {

    if (str == NULL) { // NULL

    s = new char[1];

    *s = \0;

    }

    else {

    s = new char[strlen(str) + 1];

    strcpy(s, str);

  • 44 }

    }

    String::~String()

    {

    delete s;

    }

    String::String(const String& s1) : s(NULL)

    {

    s = new char[strlen(s1.s) + 1];

    strcpy(s, s1.s);

    }

    String& String::operator=(const String& s1)

    {

    if (this == &s1) return *this;

    delete s; //

    s = new char[strlen(s1.s) + 1];

    strcpy(s, s1.s);

    return *this;

    }

    void String::Dump(ostream& os)

    {

    os

  • 45 public:

    Foo& operator=(const Foo&);

    };

    class Bar {

    public:

    // =

    };

    class FooBar {

    private:

    Foo f;

    Bar b;

    public:

    FooBar& operator=(const FooBar&);

    };

    FooBar& FooBar::operator=(const FooBar& fb)

    {

    if (this == &fb) return *this;

    f = fb.f; // = Foo

    f = fb.b; // =

    return *this;

    }

    , , =. .

    . , , . :

    class Foo {...}

    class Bar : public Foo {

    public:

    Bar& operator=(const Bar&);

    };

    Bar& Bar::operator=(const Bar& b)

    {

    if (this == &b) return *this;

    this->Foo::operator=(b); // -?

    return *this;

    }

    , (, *((Foo)this)=b;), . . , , Bar Foo . , Foo::operator= . , , Foo::operator=.

    = = . , . & X::operator=(const &) , .

  • 46 class String {

    //

    public:

    String& operator=(const String&); //

    String& operator=(char*); //

    String& operator=(int); // atoi()

    };

    = . String char* , . . , ( ) .

    C++ . , Add , + . , , . -, . -, . , .

    (, +) : . C++ operator, .

    class Foo {...}

    Foo x, y, z;

    z = x + y; // ()

    z = operator+(x, y); // ( )

    z = x.operator+(y); // ( )

    , , , , , . . . , , , . ( ! ~) . , ( , . ->).

    (, ). , , ( ). , C++. - #$%^&, C++ .

    , , , +. , , , .

  • 47

    , .

    class String {

    friend String& operator+(const String&, const String&);

    private:

    char* s;

    public:

    // ..

    }

    String& operator+(const String& s1, const String& s2)

    {

    char* s = new char[strlen(s1.s) + strlen(s2.s) + 1];

    strcat(s, s1.s, s2.s);

    String newStr(s);

    delete s;

    return newStr;

    }

    String s1 = Hello;

    String s2 = Goodbye;

    String s3 = s1 + s2;

    , ( ). . operator+ , s, : char*, , - . operator+ String, , String.

    , , =, [], () -> .

    , 1 .

    class String {

    private:

    char* s;

    public:

    // ..

    String& operator+(const String&) const;

    };

    String& String::operator+(const String& s1) const

    {

    char* s2 = new char[strlen(s1.s) + strlen(s) + 1];

    strcat(s2, s1, s);

    String newStr(s2);

    delete s2;

    return newStr;

    }

  • 48 String s1 = Hello;

    String s2 = Goodbye;

    String s3 = s1 + s2;

    . , , ? : , . :

    1. (, int double).

    2. , .

    , . , .

  • 49 {

    // , ,

    // ,

    return atoll(s);

    }

    String s(1234);

    long x = s; // operator long()

    . , , , . , long(). Foo, Foo String, operator Foo(). : , , - ? , . , . . int? , int , . , . . , , . .

    . , . , , . , . , , .

    class String {

    private:

    char* s;

    public:

    operator const char*() const { return s; }

    operator char*():

    };

    String::operator char*()

    {

    char* newStr = new char[strlen(s) + 1];

    strcpy(newStr, s);

    return newStr;

    }

    , , .

    C++ , , , . , :

    1. , .

  • 50

    2. , , . , .

    3. , .

    , , - . . , , .

    , . , . , , . , . 3, .

    -> -> . .

    class Pointer {

    private:

    Foo* f;

    public:

    Pointer(Foo* foo) : f(foo) {}

    Foo* operator->() const { return f; }

    };

    Pointer p(new Foo);

    p->MemberOfFoo();

    Foo. (*-) >, ->. ->, ; , ->. , ->. /, - , ->. , -> ( Foo*) , . , .

    class Pointer2 {

    private:

    Pointer p;

    public:

    Pointer(Foo* foo) : p(foo) {}

    Pointer operator->() const { return p; }

    };

    Pointer2 p(new Foo);

    p->MemberOfFoo();

  • 51

    -> :

    1. Pointer2::operator-> Pointer.

    2. Pointer::operator-> Foo.

    3. Foo* .

    , . . , , .

    [] [] , .

    class String {

    private:

    char* s;

    public:

    String(char*);

    char operator[](int n) const; // n-

    };

    char String::operator[](int n)

    {

    //

    return s[n];

    }

    [] , .

    struct Index3 {

    int X, Y, Z;

    Index3(int x, int y, int z) : X(x), Y(y), Z(z) {}

    };

    class Array3D { // String

    private:

    //

    public:

    String& operator[](const Index3&);

    };

    String s = anArray[Index3(17, 29, 31)];

    [] , .

    () , , . , , . , C++ . () , . , - .

  • 52 class Function {

    public:

    int operator()(char*);

    };

    int Function::operator()(char* s)

    {

    cout

  • 53 };

    void* operator new(size_t bytes, Pool* p)

    {

    return p->Allocate(bytes);

    }

    extern Pool* DefaultPool;

    Foo* f = new(DefaultPool) Foo;

    new Foo; , .

    delete delete new . , :

    1. void operator delete(void* address);

    2. void operator delete(void* address, size_t bytes);

    ; , . , ... ! :

    class Foo {

    private:

    int x;

    public:

    ~Foo(); //

    };

    class Bar : public Foo {

    private:

    int y;

    public:

    ~Bar();

    };

    Bar* b = new Bar;

    delete b; //

    Foo* f = new Bar;

    delete f; // Foo, Bar

    . , . :

    1. .

    2. .

    3. , , , .

    , , , ( , v-). ? .

    delete, new, , . , .

  • 54

    new, delete . , , , .

  • , . , , , , , C++ . , , . , , , - . . C++, .

    ? - ( ) :

    class ListNode {

    private:

    ListNode* next;

    void* data;

    public:

    ListNode(void* d, ListNode* n = NULL) : next(n), data(d) {}

    ~ListNode() { delete next; }

    void* Data() { return data; }

    ListNode* Next() { return next; }

    };

    - ?

    , void*. , , . - - :

    for (ListNode* n = listHead; n != NULL; n = n->Next())

    f((Foo*)n->Data());

    , void* , Foo*? , , , . , , . , - ? , , .

    3

  • 56

    , , . , , , , . delete void* , .

    , . void* , . , ListNode , . , .

    , . , Foo - ListOfFoos. , Foo . -, , ? , , . , .

    #define: #define ListNode(Type) \

    class ListNode##Type { \

    private: \

    ListNode##Type* next; \

    Type* data; \

    public: \

    ListNode##Type(Type* d, ListNode* n = NULL) : next(n), data(d) {} \

    ~ListNode() { delete next; } \

    void* Data() { return data; } \

    ListNode* Next() { return next; } \

    };

    \, , . ## . , . . (inline), , . . #define , , . , , - , .

    #define. , . . . C++ . . , \ ##.

  • 57

    , , (parameterized). ( ) .

    , template , Type ( ). , Type ( ) , . ListNode, , :

    template

    class ListNode {

    private:

    ListNode* next;

    Type* data;

    public:

    ListNode(Type* d, ListNode* n = NULL) : next(n), data(d) {}

    ~ListNode() { delete next; }

    Type* Data() { return data; }

    ListNode* Next() { return next; }

    };

    ListNode list = new ListNode (new Foo);

    Foo* f = list->Data(); //

    , . , .

    template.... , . , , .h. ., ( ., ).

    //

    template

    Type* fn(Type* t);

    //

    template

    Type* fn(Type* t) {

    // , Type

    //

    }

    Foo* f = fn(new Foo);

    , . , .

  • 58

    ( < >). ListNode , .

    template

    class ListNode {

    private:

    ListNode next;

    Type* data;

    public:

    ListNode(Type* d, ListNode* n = NULL);

    ~ListNode();

    Type* Data();

    ListNode* Next();

    };

    template

    ListNode::ListNode(Type* d, ListNode* n = NULL)

    : next(n), data(d)

    {

    }

    template

    ListNode::~ListNode()

    {

    delete next;

    }

    template

    Type* ListNode::Data()

    {

    return data;

    }

    template

    ListNode* ListNode::Next()

    {

    return next;

    }

    : .h. , ., . .

    < > , C++ . , , :

    1. class .

    2. .

    3. .

    , .

  • 59

    . , C++ , C++... . .

    , . , . , . , . , . -, .

    ! , , , . , . , .

    template

    class B {...};

    template

    class A {

    B* member; // !

    };

    . . , ; , .

    , . , ; , . , , APL-. , . . .

    , , 1000 . 1000 . .

    , , . , .h. .

    , - , . , , , . . , .h ?

  • 60

    , . , 99 100 , . - . , .

    . . , . ( , ), - : .

    . .

    class UnsafeNode { // ListNode

    private:

    UnsafeNode* next;

    void* data;

    public:

    UnsafeNode(void* d, UnsafeNode* n);

    virtual ~UnsafeNode();

    UnsafeNode* Next();

    void* Data();

    };

    template

    class SafeNode : private UnsafeNode {

    public:

    SafeNode(Type* d, SafeNode* n) : UnsafeNode(d, n) {}

    virtual ~SafeNode() { delete (Type*)Data(); }

    SafeNode* Next() { return (SafeNode*)UnsafeNode::Next(); }

    Type* Data() { return (Type*)UnsafeNode::Data(); }

    . , C++. Next() Data() , ; , . , . , , .

    , . , . Next() Data(), , .

    :

  • 61

    . . ; .

    UnsafeNode. SafeNode UnsafeNode SafeNode. SafeNode SafeNode ! . UnsafeNode UnsafeNode, SafeNode. .

    // SafeList.h

    class UnsafeNode; //

    template

    class SafeList { // UnsafeNode

    private:

    UnsafeNode* head;

    public:

    SafeList() : head(NULL) {}

    ~SafeList();

    UnsafeNode* Cursor(); //

    Type* Next(UnsafeNode*&); //

    void DeleteAt(UnsafeNode*&); //

    void InsertFirst(Type*); //

    void InsertBefore(UnsafeNode*&); //

    void InsertAfter(UnsafeNode*&); //

    };

    // SafeList.cpp

    class UnsafeNode { // ListNode

    private:

    UnsafeNode* next;

    void* data;

    public:

    UnsafeNode(void* d, UnsafeNode* n);

    virtual ~UnsafeNode();

    UnsafeNode* Next();

    void* Data();

    };

    SafeList , . ( InsertFirst) , . , , ( ). , . , , SafeList. , , . : UnsafeNode , . . , , , . , .

  • , . , .

    , , . , , . , , . :

    if ( ) {

    ;

    }

    else {

    - ;

    }

    , . , , . , . . , .

    ANSI : ANSI , C++, . , ? , , . , . , .

    : C++. : . : , . .

    , , , .

    , . throw. , Gotcha.

    enum Gotcha { kTooLow, kTooHigh };

    void fn(int x) throw(Gotcha) {

    4

  • 64 if (x < 0)

    throw kTooLow; //

    if (x > 1000)

    throw kTooHigh; //

    // -

    }

    . : , , . , , . Gotcha. , , , . .

    .

    void Fn() throw(int); //

    // - .cpp

    void Fn() throw(int) {

    //

    }

    , .

    , . , .

    void fn(); //

    , , . , , .

    void fn() throw(); //

    , , .

    void fn() throw(int, Exception_Struct, char*);

    , , , .

    void fn() throw;

    . , ( !) . :

  • 65 void f1(int) throw();

    void f1(int) throw(Exception); // !

    2 (, , ) (overloading) (overriding). , , ( - , ; ). .

    class Foo {

    public:

    virtual Fn() throw(int);

    };

    class Bar : public Foo {

    public:

    virtual Fn() throw(char*); // !

    };

    , . , Foo*, int, , r, .

    : . , .

    , . . unexpected(). terminate(), , , . except.h :

    typedef void (*unexpected_function)();

    unexpected_function set_unexpected(unexpected_function excpected_func);

    typedef... . set_unexpected() . set_unexpected() . , . , .

    unexpected_function my_handler(void) {

    //

    }

    { //

    unexpected_function old_handler = set_unexpected(my_handler);

    //

    set_unexpected(old_handler);

    }

    - , return . , , .

  • 66

    , try catch, (handlers).

    try {

    // ,

    }

    catch (Exception_Type t) {

    // Exception_Type

    }

    catch (...) {

    //

    }

    , ( ) , . , try-, , , . ; , .

    . catch , , .

    try- , . , , . ( ): goto . , .

    catch(int exception) {

    // -,

    throw(Help!); // char*

    }

    .

    , terminate(). , ? terminate() abort(), . set_terminate(). except.h :

    typedef void (*terminate_function)();

    termination_function set_terminate(terminate_function t_func);

    typedef... . set_terminate() , abort() terminate(). set_terminate() , set_terminate().

  • 67

    . , .

    , try/catch , , .

    {

    try {

    try {

    try {

    //

    }

    catch(...) {

    }

    }

    catch(...) {

    }

    }

    catch(...) {

    }

    }

    , .

    ! , throw. , , . , Ctrl, , , . , , ; .

    (unwinding the stack). throw catch.

    void fn() throw(int) {

    Foo aFoo;

    // - !

    throw(bad_news);

    }

    , aFoo. try- .

    {

    try {

    Bar b;

    fn(); //

    }

  • 68 catch(int exception) {

    // , , b

    }

    }

    , , try-. , . , .

    , try-, . , , . , .

    ( new) . . , , .

    class TempFoo {

    private:

    Foo* f;

    public:

    TempFoo(Foo* aFoo) : f(aFoo) {}

    ~TempFoo() { delete f; }

    };

    try {

    TempFoo tf(new Foo);

    // ..

    }

    catch(...) {

    // Foo tf

    }

    :

    class Foo {...}

    class Bar : public Foo {

    private:

    A a;

    B b;

    public:

    Bar();

    };

    Bar::Bar()

    {

    X x;

    throw(bad_news);

    Y y;

    }

  • 69

    , ( ), . r . , (Foo) ( b) , . x. , . Bar , .

    , b . Foo , b, Bar .

    , . , , .

    , . , .

    . try/catch , . .

    , , . , , - ( ). , : . , , .

    - , , . 99% , . , , , , , . , .

    throw catch, , . , . - , throw:

    f()

    {

    if (pool->Allocate(size) == NULL)

    //

    }

  • ? ++ . ( ) ++ , . , , . , . , .

    2

  • , ->, . :

    class Foo {

    public:

    void MemberOfFoo();

    };

    Foo* aFoo = new Foo;

    aFoo->MemberOfFoo();

    , -> , aFoo. C++ , , . -> , ( MemberOfFoo()). (Foo) ->. , ; ->.

    -> C++, , ( .), . 2, , .

    C++ , int, double . . , int , Integer, :

    class Integer {

    private:

    int value;

    public:

    Integer() : value(0) {}

    Integer(int v) : value(v) {}

    operator int() { return value; }

    Integer operator+(Integer i) { return Integer(value + i.value); }

    Integer operator+=(Integer i) { value += i.value; return *this; }

    // ..

    };

    int f(int);

    f(Integer(17)); // int()

    5

  • 74

    Integer , int Integer. , char* ( atoi()) . Integer int (, int), int() . . ! int Integer. , int. .

    ? , C++ ( -) , *- , ( ; , !). , . , .

    class PFoo {

    private:

    Foo* foo;

    public:

    PFoo() : foo(NULL) {}

    PFoo(Foo* f) : foo(f) {}

    operator Foo*() { return foo; }

    PFoo operator+(ptr_diff offset) { return PFoo(foo + offset); }

    PFoo operator+=(ptr_diff offset) { foo += offset; return *this; }

    ptr_diff operator-(PFoo pf) { return foo - pf.foo; }

    // ..

    };

    ! , Foo*! . , , , ptr_diff . , Foo, PFoo Foo*... ? . .

    PFoo pf(new Foo*);

    pf->MemberOfFoo(); //

    ((Foo*)pf)->MemberOfFoo(); // , !

    Foo*() PFoo , Foo*. , , -> . , , .

    , , ?

    1. , . , , . PFoo Foo .

    2. , (, size_t ptr_diff). , .

    3. ->. .

  • 75

    . , ptr_diff.

    -> , -> . , 2, PFoo ->. . f() , , , Foo*() .

    class PFoo {

    private:

    Foo* foo;

    public:

    PFoo() : foo(NULL) {}

    PFoo(Foo* f) : foo(f) {}

    operator Foo*() { return foo; }

    Foo* operator->() { return foo; }

    };

    void f(Foo*);

    PFoo pf(new Foo);

    f(pf); // operator Foo*()

    pf->MemberOfFoo(); // operator->()

    , pf->MemberOfFoo(), . -> , ->. , pf , ->. , . , , . - (, int), . , -, . . , , , :

    PFoo operator->() { return *this; }

    -> . C++ .

    , -, , Foo*: , -> , Foo* .

    .

    template

    class SP {

    private:

    Type* pointer;

    public:

  • 76 SP() : pointer(NULL) {}

    SP(Type* p) : pointer(p) {}

    operator Type*() { return pointer; }

    Type* operator->() { return pointer; }

    };

    void f(Foo*);

    Ptr pf(new Foo);

    f(pf); // operator Type*()

    pf->MemberOfFoo(); // operator->()

    , Foo. . , .

    C++ , :

    1. P, ? . .

    2. operator P()? . .

    3. , - P? ! operator Bar*() Bar*, Foo*. Ptrpf2(Foo*(pb.operator Bar*())), Bar* Foo* , .

    , , . (inline) operator Bar*() P(Foo*) , ; ( , ) , . : , , .

    , . , .

    class PVoid { // void*

    protected:

    void* addr;

    public:

    PVoid() : addr(NULL) {}

    PVoid(void* a) : addr(a) {}

    operator void*() { return addr; }

    };

    class Foo : public PVoid {

    public:

    PFoo() : PVoid() {}

    PFoo(Foo* p) : PVoid(p) {}

    operator Foo*() { return (Foo*)addr; }

    Foo* operator->() { return (Foo*)addr; }

  • 77 };

    class Pbar : public PFoo {

    public:

    PBar() : PFoo() {}

    PBar(Bar* p) : PFoo(p) {}

    operator Bar*() { return (Bar*)addr; }

    Bar* operator->() { return (Bar*)addr; }

    };

    pBar pb(new Bar);

    pFoo pf(pb); // , PBar PFoo

    pf->MemberOfFoo(); // PFoo::operator->

    , / ( ) , r::operator->() PFoo::operator->(). , , Ptr.

    , / . , ++ --.

    template

    class Ptr {

    private:

    Type* pointer;

    public:

    Ptr() : pointer(NULL) {}

    Ptr(Type* p) : pointer(p) {}

    operator Type*() { return pointer; }

    ptr_diff operator-(Ptr p) { return pointer p.pointer; }

    ptr_diff operator-(void* v) { return ((void*)pointer) v; }

    Ptr operator-(long index) { return Ptr(pointer index); }

    Ptr operator-=(long index) { pointer -= index; return *this; }

    Ptr operator+(long index) { return Ptr(pointer + index); }

    Ptr operator+=(long index) { pointer += index; return *this; }

    };

    , ptr_diff . , , . , . : C++, , , . , .

    - , , , . , , . , .

  • 78

    ? , , , . *-; . C++ , .

    template

    class Ptr {

    private:

    Type* pointer;

    public:

    Ptr() : pointer(NULL) {}

    Ptr(Type* p) : pointer(p) {}

    operator Type*() { return pointer; }

    Type* operator->() { return pointer; }

    };

    , -> . , . . , .

    , , . , , , .

    NULL :

    template

    class SPN {

    private:

    Type* pointer;

    public:

    SPN() : pointer(NULL) {}

    SPN(Type* p) : pointer(p) {}

    operator Type*() { return pointer; }

    Type* operator->()

    {

    if (pointer == NULL) {

    cerr

  • 79

    , , NULL. .

    #indef , , if- #ifdef, . -> .

    . :

    template

    class Ptr {

    private:

    Type* pointer;

    public:

    enum ErrorType { DereferenceNil };

    Ptr() : pointer(NULL) {}

    Ptr(Type* p) : pointer(p) {}

    operator Type*() { return pointer; }

    Type* operator->() throw(ErrorType)

    {

    if (pointer == NULL) throw DereferenceNil;

    return pointer;

    }

    };

    ( ErrorType , ; .) . , .

    , (screamer). , - NULL.

    template

    class AHHH {

    private:

    Type* pointer;

    static type* screamer;

    public:

    AHHH() : pointer(NULL) {}

    AHHH(Type* p) : pointer(p) {}

    Operator Type*() { return pointer; }

    Type* operator->()

    {

    if (p == NULL) return screamer;

    return pointer;

  • 80 }

    };

    ? . , screamer Type* , ( ) rr . , .

    , . operator Type*() operator->(), . , .

    (out-of-line) .

    template

    class PTracer {

    private:

    Type* pointer;

    public:

    PTracer() : pointer(NULL) {}

    PTracer(Type* p) : pointer(p) {}

    operator Type*();

    Type* operator->();

    };

    template

    #ifdef DEBUGGING

    inline

    #endif

    PTracer::operator Type*()

    {

    return pointer; //

    }

    template

    #ifdef DEBUGGING

    inline

    #endif

    Type* PTracer::operator->()

    {

    return pointer; //

    }

    , .

    -> cout cerr, .

  • 81

    Type* -> .

    template

    class SPCS {

    private:

    Type* pointer;

    static int conversions;

    static int members;

    public:

    SPCS() : pointer(NULL) {}

    SPCS(Type* p) : pointer(p) {}

    operator Type*() { conversions++; return pointer; }

    Type* operator->() { members++; return pointer; }

    int Conversions() { return conversions; }

    int Members() { return members; }

    };

    - . Foo.cpp: // Foo.cpp

    int Ptr::conversions = 0;

    int Ptr::members = 0;

    , #ifdef, .

    . , (master pointers), , . , ( ), () . . , , . , , Counter.

    class Counter {

    protected:

    Counter() : conversions(0), members(0) {}

    Counter(const Counter&) : conversions(0), members(0) {}

    Counter& operator=(const Counter&) { return *this; }

    public:

    int conversions;

    int members;

    int Conversions() { return conversions; }

    int Members() { return members; }

    };

    template

    class SPOP {

    private:

    Type* pointer;

  • 82 public:

    SPOS() : pointer(NULL) {}

    SPOP(Type* f) : pointer(f) {}

    operator Type*() { pointer->conversions++; return pointer; }

    Type* operator->() { pointer->members++; return pointer; }

    };

    , .

    , , . , ReadObject() , pointer. , .

    typedef unsigned long DiskAddress; //

    template

    class CP {

    private:

    DiskAddress record_number;

    Type* pointer;

    void ReadObject(); //

    public:

    CP(DiskAddress da) : pointer(NULL), record_number(da) {}

    CP(Type* f) : pointer(f), record_number(f->RecordNumber()) {}

    operator Type*()

    {

    if (pointer == NULL) this->ReadObject();

    return pointer;

    }

    Type* operator->()

    {

    if (pointer == NULL) this->ReadObject();

    return pointer;

    }

    };

    , , . , , , , , . , , , .

    . :

    1. .

    2. , new, .

  • 83

    , . ( ), 31 . , 32 . , , , , , .

    : , . C++, , , .

  • . , . , ? ( )? ? ? ? .

    . , ,