banner

terns. The layouts are very similar and share lots of common aspects; so, to avoid repeating ourselves a whole bunch, we’ll look at the common stuff first, and then focus on the differences.

First of all, we need some hierarchical data. In the book repository, I’ve provided a file called GoT-linecture as follows:

tips

{

“0”: {

“0”: 5,

“-1”: 2

},

“-1”: {

“0”: 3,

“-1”: 4

}

}

Not very self-explanatory, but that’s a collection of columns containing rows. The (0, 0) square contains 5 primes, (-1, 0) contains 2, and so on.

To get the median and the number of shades,bool WindowsFile::Close() { … } …

有时,一个程序虽然定义了接口,但是只提供了一种实现。在这种情况下,通过移除接口,即移除 file.h 类定义中的 virtual 关键字并提供 file 的成员函数的实现,可以节省虚函数调用(特别是频繁地对 GetChar() 的调用)的开销。

停下来思考

正如前一节中所提到的,开发人员清晰地定义接口的愿景(这当然是好事)与性能优化开发人员改善性能(如果 GetChar() 被分析器标记为热点函数,那么也有问题)的渴求之间存在矛盾。在程序稳定后,判断有无其他实现方法会更加容易。这里的知识可以帮助我们选