Вы находитесь на странице: 1из 5

,

.
N4.
:
: 15.07.2013
++,
. : N1, N2, N3.


, , .
:
class Base { ... };
class X : public Base { ... };
class Y : public Base { ... };
class XY : public X, public Y { ... };
. 'Base' . 'XY'
'Base'. :

1. .
.
'XY' 2.
class Base { ... };
class X : public virtual Base { ... };
class Y : public virtual Base { ... };
class XY : public X, public Y { ... };

2. .
'Base', , 'XY'.
, . , 'X' 'Y'
'Base'. , .
,
.
" " 'XY' ,
'Base'.
.
'XY' 'Base' 'X' 'Y'.
'X', 'Y', 'XY'.
'Base' 'XY',
'X' 'Y'. , .
,
'X' 'Y', 'Base'.
, .
:
X::X(int A) : Base(A) {}
Y::Y(int A) : Base(A) {}
XY::XY() : X(3), Y(6) {}
? 3 6?
.
'XY' 'Base', .
'Base' .
'XY' 'X' 'Y', 'Base' .
'Base' - .
.
. , ,

. .
, 'Base'.

,
'Base'. :
XY &XY::operator =(const XY &src)
{
if (this != &src)
{
X::operator =(*this);
Y::operator =(*this);
....
}
return *this;
}
'Base'. , 'X' 'Y'
, 'Base'.
'Base' . :
XY &XY::operator =(const XY &src)
{
if (this != &src)
{
Base::operator =(*this);
X::PartialAssign(*this);
Y::PartialAssign(*this);
....
}
return *this;
}
, . ,
.


- ,
:

Base *b = Get();
XY *q = static_cast<XY *>(b); //
XY *w = (XY *)(b); //
, - ,
'reinterpret_cast':
XY *e = reinterpret_cast<XY *>(b);
.
'Base' , 'XY'. , .
3.
, dynamic_cast.
, dynamic_cast, .

3. .

?
, .
.
.
" " . ,
.
,
.
.
.
dynamic_cast. , .
, , ,
. , ,
. , .
.

. .
, .
, .
, .


,
, . , .
, : .
, " ...
" [3].
. , , .
, . ,
.
, - . ,
, .
.


1. . . C++.
. - .: . - 264 .: . 32.973.26-018.2, ISBN 9785-94074-837-3. (. 45 53).
2. Wikipedia. ().
3. . . " ... ". (
. 101 ).

Вам также может понравиться