Archive for October, 2007

Use base class iterator in a derived class with a template

Thursday, October 18th, 2007

To tell the truth, I am still a newbie in C++. So I decided to take a evening and do a quick exercise. What I am trying to do is to write a class which uses the iterator from its ‘parent’ class:

 1: #include <vector>
 2: #include <iostream>
 3:  
 4: template <typename T>
 5: class myVector : public std::vector<T>
 6: {
 7: public:
 8:  void Test();
 9: };
 10:  
 11: template <typename T>
 12: void myVector<T>::Test()
 13: {
 14:  typename std::vector<T>::iterator i;
 15:  
 16:  for (i = this->begin(); i != this->end(); i++)
 17:  std::cout << *i << std::endl;
 18:  
 19:  return;
 20: }
 21:  
 22: int main()
 23: {
 24:  myVector<int> v;
 25:  v.push_back(1);
 26:  v.push_back(2);
 27:  v.push_back(3);
 28:  v.push_back(4);
 29:  v.Test();
 30:  return 0;
 31: }
 

The whole point here is the typename in line 14. That is the tricky part which took me a while to figure out. Without it the code won’t even compile. Now I can add all different kinds of sorting methods to myVector. Yes, I know it’s a bad idea. Just code for fun.

不能不说的秘密

Monday, October 15th, 2007

秋天不知道什么时候已经等在了窗外 让门前的那棵屋树霎时间红了半个身子 一片突然掉落的霜叶 让人还以为是二月的花

天气变化的厉害 思绪和身子骨一样都不由地得瑟个不停 终于明白 为什么老毛对待敌人的时候 一定要用一招叫做秋风扫落叶的掌法 才稍微被风吹一下 我就吸着鼻涕 想起家来

Don’t write multiple statements in one line

Saturday, October 13th, 2007

… for several reasons:

  1. It doesn’t boost the performance.
  2. It doesn’t reduce the memory usage.
  3. It is hard to set a breakpoint while debugging.
  4. It reduces the readability.
  5. It is nothing to show off.

Counter example:

Don’t write multiple statements in one line for that, it doesn’t boost the performance and reduce the memory usage, but hard to set a breakpoint while debugging as well as reducing the readability, plus it is nothing to show off.


Chat with me. =)