exception the last one.

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;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值