C++编程思想 第1卷 第7章 函数重载与默认参数 重载的例子

重载函数的改写
重载重要的应用是构造函数

//: C07:Stash3.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Function overloading
#ifndef STASH3_H
#define STASH3_H

class Stash {
  int size;      // Size of each space
  int quantity;  // Number of storage spaces
  int next;      // Next empty space
  // Dynamically allocated array of bytes:
  unsigned char* storage;
  void inflate(int increase);
public:
  Stash(int size); // Zero quantity
  Stash(int size, int initQuantity);
  ~Stash();
  int add(void* element);
  void* fetch(int index);
  int count();
};
#endif // STASH3_H ///:~

Stash()的第一个构造函数与前面一样,第二个有一个Quantity参数指明
分配内存位置的初始大小

//: C07:Stash3.cpp {O}
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Function overloading
#include "Stash3.h"
#include "../require.h"
#include <iostream>
#include <cassert>
using namespace std;
const int increment = 100;

Stash::Stash(int sz) {
  size = sz;
  quantity = 0;
  next = 0;
  storage = 0;
}

Stash::Stash(int sz, int initQuantity) {
  size = sz;
  quantity = 0;
  next = 0;
  storage = 0;
  inflate(initQuantity);
}

Stash::~Stash() {
  if(storage != 0) {
    cout << "freeing storage" << endl;
    delete []storage;
  }
}

int Stash::add(void* element) {
  if(next >= quantity) // Enough space left?
    inflate(increment);
  // Copy element into storage,
  // starting at next empty space:
  int startBytes = next * size;
  unsigned char* e = (unsigned char*)element;
  for(int i = 0; i < size; i++)
    storage[startBytes + i] = e[i];
  next++;
  return(next - 1); // Index number
}

void* Stash::fetch(int index) {
  require(0 <= index, "Stash::fetch (-)index");
  if(index >= next)
    return 0; // To indicate the end
  // Produce pointer to desired element:
  return &(storage[index * size]);
}

int Stash::count() {
  return next; // Number of elements in CStash
}

void Stash::inflate(int increase) {
  assert(increase >= 0);
  if(increase == 0) return;
  int newQuantity = quantity + increase;
  int newBytes = newQuantity * size;
  int oldBytes = quantity * size;
  unsigned char* b = new unsigned char[newBytes];
  for(int i = 0; i < oldBytes; i++)
    b[i] = storage[i]; // Copy old to new
  delete [](storage); // Release old storage
  storage = b; // Point to new memory
  quantity = newQuantity; // Adjust the size
} ///:~

用第一个构造函数时,没有内存分配给storage,内存时第一次调用add()
来增加一个对象时分配的

//: C07:Stash3Test.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
//{L} Stash3
// Function overloading
#include "Stash3.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main() {
  Stash intStash(sizeof(int));
  for(int i = 0; i < 100; i++)
    intStash.add(&i);
  for(int j = 0; j < intStash.count(); j++)
    cout << "intStash.fetch(" << j << ") = "
         << *(int*)intStash.fetch(j)
         << endl;
  const int bufsize = 80;
  Stash stringStash(sizeof(char) * bufsize, 100);
  ifstream in("Stash3Test.cpp");
  assure(in, "Stash3Test.cpp");
  string line;
  while(getline(in, line))
    stringStash.add((char*)line.c_str());
  int k = 0;
  char* cp;
  while((cp = (char*)stringStash.fetch(k++))!=0)
    cout << "stringStash.fetch(" << k << ") = "
         << cp << endl;
  getchar();
} ///:~

两个构造函数都会被执行

stringStash调用构造函数,使用第二个参数。

把Stash3Test.cpp放到工程目录
输出
intStash.fetch(3) = 3
intStash.fetch(4) = 4
intStash.fetch(5) = 5
intStash.fetch(6) = 6
intStash.fetch(7) = 7
intStash.fetch(8) = 8
intStash.fetch(9) = 9
intStash.fetch(10) = 10
intStash.fetch(11) = 11
intStash.fetch(12) = 12
intStash.fetch(13) = 13
intStash.fetch(14) = 14
intStash.fetch(15) = 15
intStash.fetch(16) = 16
intStash.fetch(17) = 17
intStash.fetch(18) = 18
intStash.fetch(19) = 19
intStash.fetch(20) = 20
intStash.fetch(21) = 21
intStash.fetch(22) = 22
intStash.fetch(23) = 23
intStash.fetch(24) = 24
intStash.fetch(25) = 25
intStash.fetch(26) = 26
intStash.fetch(27) = 27
intStash.fetch(28) = 28
intStash.fetch(29) = 29
intStash.fetch(30) = 30
intStash.fetch(31) = 31
intStash.fetch(32) = 32
intStash.fetch(33) = 33
intStash.fetch(34) = 34
intStash.fetch(35) = 35
intStash.fetch(36) = 36
intStash.fetch(37) = 37
intStash.fetch(38) = 38
intStash.fetch(39) = 39
intStash.fetch(40) = 40
intStash.fetch(41) = 41
intStash.fetch(42) = 42
intStash.fetch(43) = 43
intStash.fetch(44) = 44
intStash.fetch(45) = 45
intStash.fetch(46) = 46
intStash.fetch(47) = 47
intStash.fetch(48) = 48
intStash.fetch(49) = 49
intStash.fetch(50) = 50
intStash.fetch(51) = 51
intStash.fetch(52) = 52
intStash.fetch(53) = 53
intStash.fetch(54) = 54
intStash.fetch(55) = 55
intStash.fetch(56) = 56
intStash.fetch(57) = 57
intStash.fetch(58) = 58
intStash.fetch(59) = 59
intStash.fetch(60) = 60
intStash.fetch(61) = 61
intStash.fetch(62) = 62
intStash.fetch(63) = 63
intStash.fetch(64) = 64
intStash.fetch(65) = 65
intStash.fetch(66) = 66
intStash.fetch(67) = 67
intStash.fetch(68) = 68
intStash.fetch(69) = 69
intStash.fetch(70) = 70
intStash.fetch(71) = 71
intStash.fetch(72) = 72
intStash.fetch(73) = 73
intStash.fetch(74) = 74
intStash.fetch(75) = 75
intStash.fetch(76) = 76
intStash.fetch(77) = 77
intStash.fetch(78) = 78
intStash.fetch(79) = 79
intStash.fetch(80) = 80
intStash.fetch(81) = 81
intStash.fetch(82) = 82
intStash.fetch(83) = 83
intStash.fetch(84) = 84
intStash.fetch(85) = 85
intStash.fetch(86) = 86
intStash.fetch(87) = 87
intStash.fetch(88) = 88
intStash.fetch(89) = 89
intStash.fetch(90) = 90
intStash.fetch(91) = 91
intStash.fetch(92) = 92
intStash.fetch(93) = 93
intStash.fetch(94) = 94
intStash.fetch(95) = 95
intStash.fetch(96) = 96
intStash.fetch(97) = 97
intStash.fetch(98) = 98
intStash.fetch(99) = 99
stringStash.fetch(1) = //: C07:Stash3Test.cpp
stringStash.fetch(2) = // From Thinking in C++, 2nd Edition
stringStash.fetch(3) = // Available at http://www.BruceEckel.com
stringStash.fetch(4) = // (c) Bruce Eckel 2000
stringStash.fetch(5) = // Copyright notice in Copyright.txt
stringStash.fetch(6) = //{L} Stash3
stringStash.fetch(7) = // Function overloading
stringStash.fetch(8) = #include "Stash3.h"
stringStash.fetch(9) = #include "../require.h"
stringStash.fetch(10) = #include <fstream>
stringStash.fetch(11) = #include <iostream>
stringStash.fetch(12) = #include <string>
stringStash.fetch(13) = using namespace std;
stringStash.fetch(14) =
stringStash.fetch(15) = int main() {
stringStash.fetch(16) =   Stash intStash(sizeof(int));
stringStash.fetch(17) =   for(int i = 0; i < 100; i++)
stringStash.fetch(18) =     intStash.add(&i);
stringStash.fetch(19) =   for(int j = 0; j < intStash.count(); j++)
stringStash.fetch(20) =     cout << "intStash.fetch(" << j << ") = "
stringStash.fetch(21) =          << *(int*)intStash.fetch(j)
stringStash.fetch(22) =          << endl;
stringStash.fetch(23) =   const int bufsize = 80;
stringStash.fetch(24) =   Stash stringStash(sizeof(char) * bufsize, 100);
stringStash.fetch(25) =   ifstream in("Stash3Test.cpp");
stringStash.fetch(26) =   assure(in, "Stash3Test.cpp");
stringStash.fetch(27) =   string line;
stringStash.fetch(28) =   while(getline(in, line))
stringStash.fetch(29) =     stringStash.add((char*)line.c_str());
stringStash.fetch(30) =   int k = 0;
stringStash.fetch(31) =   char* cp;
stringStash.fetch(32) =   while((cp = (char*)stringStash.fetch(k++))!=0)
stringStash.fetch(33) =     cout << "stringStash.fetch(" << k << ") = "
stringStash.fetch(34) =          << cp << endl;
stringStash.fetch(35) =   getchar();
stringStash.fetch(36) = } ///:~

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/81141882