T t;
T t();
T t(u);
T t = u; // T t(T(u));
// When using return-by-value for non-builtin return types, prefer returning a const value.
typedef int bool;
const bool true = 1;
const bool false = 0;
void f(int);
void f(bool); // redefined. and bool b = 3. b != true && b != false;
// Option 2: #define
#define bool int
#define true 1
#define false 0 // if already defined in these include files.
// Option 3: enum
enum bool { false, true };
bool b;
b = ( i == j ); // error.
// 4
class bool
{
public:
bool();
bool( int ); // to enable conversions from
bool& operator=( int ); // conditional expressions
//operator int(); // questionable!
//operator void*(); // qusestionable! e.g. if(b);
private:
unsigned char b_;
};
const bool true ( 1 );
const bool false( 0 );
// forwarding funcions
// file f.cpp
#include "f.h"
/*...*/
bool f( X x )
{
return g( x );
}
/* ******************************************************************************************** */
#include <cassert>
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
// The following lines come from other header files.
char* itoa( int value, char* workArea, int radix );
// Avoid using global or static objects. If you must use a global or static object,
// always be very careful about the order-of-initialization rules.
extern int fileIdCounter;
// Helpers to automate class invariant checking.
//
template<class T>
inline void AAssert( T& p )
{
static int localFileId = ++fileIdCounter;
if( !p.Invariant() )
{
cerr << "Invariant failed: file " << localFileId
<< ", " << typeid(p).name()
<< " at " << static_cast<void*>(&p) << endl;
assert( false );
}
}
template<class T>
class AInvariant
{
public:
AInvariant( T& p ) : p_(p) { AAssert( p_ );
~AInvariant() { AAssert( p_ ); }
private:
T& p_;
};
#define AINVARIANT_GUARD AInvariant<AIType> invariantChecker( *this )
//-------------------------------------------------------------
template<class T>
class Array : private ArrayBase, public Container
{
typedef Array AIType;
public:
Array( size_t startingSize = 10 ) : Container( startingSize ), ArrayBase( Container::GetType() ),
used_(0), size_(startingSize), buffer_(new T[size_])
{
AINVARIANT_GUARD;
}
void Resize( size_t newSize )
{
AINVARIANT_GUARD;
T* oldBuffer = buffer_;
buffer_ = new T[newSize];
copy( oldBuffer, oldBuffer+min(size_,newSize), buffer_ );
delete[] oldBuffer;
size_ = newSize;
}
string PrintSizes()
{
AINVARIANT_GUARD;
char buf[30];
return string("size = ") + itoa(size_,buf,10) +
", used = " + itoa(used_,buf,10);
}
bool Invariant()
{
if( used_ > 0.9*size_ ) Resize( 2*size_ );
return used_ <= size_;
}
private:
T* buffer_;
size_t used_, size_;
};
int f( int& x, int y = x ) { return x += y;
int g( int& x ) { return x /= 2; }
int main( int, char*[] )
{
int i = 42;
cout << "f(" << i << ") = " << f(i) << ", "
<< "g(" << i << ") = " << g(i) << endl;
Array<char> a(20);
cout << a.PrintSizes() << endl;
}
exception the last one.
最新推荐文章于 2021-11-19 21:26:30 发布