LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 808|回复: 4

Play with C++

[复制链接]
发表于 2005-9-14 10:12:20 | 显示全部楼层 |阅读模式
1.How to get the size of an array?

  1. template<typename T,unsigned int size>
  2. unsigned int test(T(&arr)[size])
  3. {
  4.     return size;
  5. }
复制代码



2.How to implement a final class?

  1. class CFinal
  2. {
  3.     CFinal(){}
  4. public:
  5.     static CFinal* new_object()
  6.     {
  7.         return new CFinal;
  8.     }
  9.     static CFinal get_instance()
  10.     {
  11.         return CFinal();
  12.     }
  13. };
复制代码


3.How to fix the wrong input when we need an integer?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6.     int apple;
  7.     cout<<"Please input an integer:"<<endl;
  8.     cin>>apple;
  9.     while(!cin)
  10.     {
  11.         cout<<"Wrong! Input again!"<<endl;
  12.         cin.clear();
  13.         string tmp;
  14.         cin>>tmp>>apple;
  15.     }
  16.     cout<<apple<<endl;
  17.     return 0;
  18. }
复制代码
发表于 2005-9-14 18:46:02 | 显示全部楼层
final class的实现不太自然吧?没法用通用的语法如

  1. CFinal cf;
  2. CFinal* cf = new CFinal();
复制代码

还是Bjarne Stroustrup的方法好

  1.         class Usable;

  2.         class Usable_lock {
  3.                 friend class Usable;
  4.         private:
  5.                 Usable_lock() {}
  6.                 Usable_lock(const Usable_lock&) {}
  7.         };

  8.         class Usable : public virtual Usable_lock {
  9.                 // ...
  10.         public:
  11.                 Usable();
  12.                 Usable(char*);
  13.                 // ...
  14.         };

  15.         Usable a;

  16.         class DD : public Usable { };

  17.         DD dd;  // error: DD::DD() cannot access
  18.                 // Usable_lock::Usable_lock(): private  member
复制代码

详见
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

而且,有了tr1,C风格数组几乎不需要了,用array的效率一样。当然legacy code要这样弄
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-14 20:09:59 | 显示全部楼层
Post by manphiz

还是Bjarne Stroustrup的方法好

  1.         class Usable;

  2.         class Usable_lock {
  3.                 friend class Usable;
  4.         private:
  5.                 Usable_lock() {}
  6.                 Usable_lock(const Usable_lock&) {}
  7.         };

  8.         class Usable : public virtual Usable_lock {
  9.                 // ...
  10.         public:
  11.                 Usable();
  12.                 Usable(char*);
  13.                 // ...
  14.         };

  15.         Usable a;

  16.         class DD : public Usable { };

  17.         DD dd;  // error: DD::DD() cannot access
  18.                 // Usable_lock::Usable_lock(): private  member
复制代码

详见
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

excellent
而且,有了tr1,C风格数组几乎不需要了,用array的效率一样。当然legacy code要这样弄

just for fun, just play with it
回复 支持 反对

使用道具 举报

发表于 2005-9-15 11:15:59 | 显示全部楼层
Post by rickxbx
1.How to get the size of an array?

  1. template<typename T,unsigned int size>
  2. unsigned int test(T(&arr)[size])
  3. {
  4.     return size;
  5. }
复制代码



对动态分配的数组管用吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-15 12:18:43 | 显示全部楼层
Post by daemeon
对动态分配的数组管用吗?

I'm afraid not,
动态分配的数组在程序员看来只是一个指针而已
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表