,
, . ,
String Stirng::strin(int)
:
class Stirng {
friend class
Buf;
public:
String( char* );
String();
private:
String( int
); // ... rest of Stirng class };
String
Buf String,
int.
String,
char*.
f() { // ok: String::String( char* ) is
public
String search( "rosebud" ); { // ok: String::String( char* ) is
public
private
String inBuf( 1024 );
... }
Buf::in() // error:
String::String( int ) is
String search( "rosebud" );
// ok:
String::String( int ) is private
// Buf is a friend to String
String inBuf
( 4096 );
... }
.
.
IntItem, 5.2 .
IntItem IntList,
IntItem.
++
, ,
. ,
, , ,
delete .
, ,
. ,
; . String
:
class String { public:
~String();
//
destructor
... };
,
(~). (
).
. String
:
String::~String() {delete str; }
,
. -
- , .
. -
,
. , str ,
new String .
len, , .
.
, ,
, :
String::~String()
{
#ifdef DEBUG
cout << "String() " << len << " " str
<< "n";
#endif
delete str; }
,
,
.
o , .
-, delete.
. ,
#include
"String.h"
String search( "rosebud" );
f() { // would not want destructor
applied to p
String *p = &search;
// would want destructor applied to
p
String *pp = new String( "sleigth" );
// ... body of f() //
Sting::String() invoke for pp
delete pp; }
,
delete, (.. 0),
.
if ( pp != 0
)
delete pp;
,
. ,
, . ,
new.
,
#include <string.h>
#include
<stream.h>
#include <new.h>
struct inBuf {
public:
inBuf( char* );
~inBuf();
private: char *st;
int
sz;
};
inBuf::inBuf( char *s ) {
st = new char [ sz = strlen(s)+1
];
strcpy( st, s );
}
inBuf::~inBuf()
{
cout<<"inBuf::~inBuf(): " << st <<"n";
delete
st;
}
char *pBuf = new char[ sizeof( inBuf ) ];
main() {
inBuf *pb = new (pBuf) inBuf( "free store inBuf #1"
);
pb->inBuf::~inBuf();
// explicit destructor call
pb = new
(pBuf) inBuf( "free store inBuf #2" );
pb- >inBuf::~inBuf();
//
explicit destructor call // ... }
:
inBuf::~inBuf(): free
store
inBuf #1 inBuf::~inBuf(): free store
inBuf #2
.
,
pb->inBuf::~inBuf();
//
correct
pb->~inBuf();
// error