.


:




:

































 

 

 

 





. , .

: Vector, :

class Vector{

public:

class Size{}; //

enum {max = 32000}; //

Vector (int n){ //

if (n<0 || n>max) throw Size();

...

}

...

};

Vector Size:

try{

Vector * = new Vector(i);

...

}

catch (Vector::Size){

... //

}

. , , , . , .

, :

,

, ,

.

, , . new , - .

. , , , .

, . , , .

: .

class Matherr{};

class Overflow: public Matherr{}; //

class Underflow: public Matherr{}; //

class ZeroDivide: public Matherr{}; //

/ :

class IOerr{};

class Readerr: public IOerr{}; //

class Writerr: public IOerr{}; //

class Seekerr: public IOerr{}; //

, , .

, C++. exception, <stdexcept>. , new bad_alloc.

, .

 

2.16

: ++

 

++ , , const_cast, dynamic_cast, reinterpret_cast static_cast.

const_cast const.

:

const_cast <> ();

, , const ( ).

, , . , const, .. . const , . const_cast , . , , , .

:

void print (int *p) { // *

cout << *;

}

const int *p;

print(p); // ,

 

void print (int *p) { // *

cout << *;

}

const int *p;

print(const_cast<int*>p); // ,

const_cast , , , , . , const, .

static_cast :

,

,

.

, static_cast .

:

static_cast <> ();

, , . , .

:

float f = 100;

int i = static_cast <int> (f); //

, , .

reinterpret_cast (, ), void* . , .

:

reinterpret_cast <> ();

, , .

:

char * = reinterpret_cast <char*>(malloc(100));

long l = reinterpret_cast <long>(p);

static_cast reinterpret_cast , , . static_cast, - reinterpret_cast.

dynamic_cast , - .

:

dynamic_cast < *> ();

, - .

- . , , , - , y, bad_cast.

(downcast), - (upcast) .. . , , (crosscast).

dynamic_cast :

class { / *... * / };

class : public { /*... */ };

* = new ;

* b = dynamic_cast<B*>(c); // * b = ;

dynamic_cast - .

, . , . RTTI (run-time type information) - . dynamic_cast , .. .

dynamic_cast , , .

, , , .. , , , , dynamic_cast .

dynamic_cast .

RTTI typeinfo.h .

:

, f2. dynamic_cast , f2 demo, , , .

#include <iostream.h>

#include <typeinfo.h>

class B{

...

public: virtual void f1 () {...};

};

class C: public B {

...

public: void f2() {cout << "f2";}

};

void demo (B* p) {

C* = dynamic_cast<C*>(p);

if (c) c->f2();

else cout << " ";

}

 

int main(){

* b = new ;

demo(b); // " "

* = new :

demo(c); // "f2" ()

return 0;

}

dynamic_cast :

* = (*) ;

. , .

- . dynamic_cast , .

: .

, :

#include <iostream.h>

#include <typeinfo.h>

class A{

...

public: virtual ~A(){... };

};

class B: public virtual A{... };

class C: public virtual A{... };

class D: public B, public C{... };

void demo(A *a) {

D* d = dynamic_cast<D*>(a);

if (d) {... }

}

int main(){

D *d = new D;

demo(d);

return 0;

}

- , . , dynamic_cast .

, bad_cast.

:

#include <iostream.h>

#include <typeinfo.h>

class B{

...

public: virtual void fl () {... };

};

class C: public B{

...

public: void f2(){... };

};

 

 

void demo (B& p) {

try{

C& = dynamic_cast<C&>(p);

c.f2();

}

catch (bad_cast) {

...

}

}

 

int main(){

B* b = new B;

demo(*b); //

* = new ;

demo(*c); //

return 0;

}





:


: 2016-11-12; !; : 1510 |


:

:

.
==> ...

1661 - | 1465 -


© 2015-2024 lektsii.org - -

: 0.027 .