Robin Chow's C++ Blog

 

[导入]C# 中字符和字节相互转换

将字符转换为字节:
byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());

将字节转换为字符:
string sTime = Encoding.ASCII.GetString(bytes,0,bytesRead);
文章来源:http://my.donews.com/robinchow/2007/03/25/c-%e4%b8%ad%e5%ad%97%e7%ac%a6%e5%92%8c%e5%ad%97%e8%8a%82%e7%9b%b8%e4%ba%92%e8%bd%ac%e6%8d%a2/

posted @ 2007-10-23 20:45 Robin Chow 阅读(111) | 评论 (0)编辑 收藏

虚拟函数和重载

如下面的程序:

 1     #include < iostream >
 2 
 3     using namespace std;
 4 
 5     class base{
 6     public:
 7         virtual void f(char i){
 8             cout << "Base class: " << i << endl;
 9         };
10         virtual void f(double d){
11             cout << "Base class: " << d << endl;
12         }
13     };
14 
15     class derived : public base{
16     public:
17         void f(char i){
18             cout << "Derived class: " << i << endl;
19         }
20     };
21 
22     int main(int argc, char** argv)
23     {
24         base *pb = new derived;
25         pb->f(2.0);
26         delete pb;
27         return 0;
28     }
29 


开始的时候感觉有点迷惑,被调用的是 base 的 f 呢?还是 derived 的 f(需要进行参数类型转换)。实验证明,被调用的是 base 的 f。


因为重载是根据静态类型来选择函数的,亦即由 pb 本身的类型来决定。而虚拟则是根据动态类型来选择函数,由 pb 指向的对象类型决定。因此编译器首先根据重载来选择一个基类的函数,如果该函数为虚拟函数且被派生类实现,则再通过多态机制调用派生类函数。

posted @ 2007-01-13 14:42 Robin Chow 阅读(187) | 评论 (0)编辑 收藏

仅列出标题
共9页: 1 2 3 4 5 6 7 8 9 

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜