Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > c++只支持单分派(single dispatch)
【标  题】:c++只支持单分派(single dispatch)
【关键字】:c++,single,dispatch
【来  源】:http://www.cppblog.com/jacky2019/archive/2007/03/09/19476.html

c++只支持单分派(single dispatch)

Your Ad Here
居然写错了!哈哈。谢谢各位大哥的评论阿。这个代码是我以前写的,我还以为是正确的。。。晕了。哈哈
下面的代码是改过以后的,不知道还有没有问题?希望各位多提意见哈
#include <iostream>
using namespace std;
//B
class B
{
public:
?virtual void print()
?{cout << "B" << endl;}
};
class BE : public B
{
public:
?void print()
?{cout << "BE" << endl;}
?void print(int i)
?{cout << "BE:" << i << endl;}
};

//A
class A
{
public:
?void output(int i)
?{
??? ?B * b = new B;
??? ?B * be = new BE;
??? ?b->print();
??? ?be->print();
??? ?be->print(i);//问题出现在这里!!!如改为((BE*)be)->print(i)就OK了。
?}
};

int main()
{
?A a;
?a.output(99);
?
?return 0;
}

--------------------Configuration: test - Debug--------------------
Compiling source file(s)...
test.cpp
test.cpp: In member function `void A::output(int)':
test.cpp:29: error: no matching function for call to `B::print(int&)'
test.cpp:8: note: candidates are: virtual void B::print()

test.exe - 2 error(s), 0 warning(s)




//////////////////////////////////////////////////////////////////////////////////////////////////////
以下代码有问题!
//////////////////////////////////////////////////////////////////////////////////////////////////////
c++不支持双分派(double dispatch)和多分派(multi-dispatch),只支持单分派(single dispatch)。一个典型的不支持双分派的例子如下所示:
#include <iostream>
using namespace std;
class B
{
public:
?virtual void print()
?{cout << "c" << endl;}
};
class E1 : public B
{
public:
?void print()
?{cout << "E1" << endl;}
};
class E2 : public B
{
public:
?void print()
?{cout << "E2" << endl;}
};
class A
{
public:
?virtual void output(B* p)
?{cout << "A" << endl;p->print();}
};
class D1 : public A
{
public:
?void output(B* p)
?{cout << "D1" << endl;p->print();}
};
class D2 : public A
{
public:
?void output(B* p)
?{cout << "D2" << endl;p->print();}
};
void f(A* p, B* q)
{
?p->output(q);
}
int main()
{
?A * pd1 = new D1;
?B * pe1 = new E1;
?
?f(pd1, pe1);
?
?end:
?int end;
?cin >> end;
?return 0;
}
ACE_CDR::mb_align(ACE_Message_Block * mb)使用问题:【上一篇】
TAU G2错误信息:Dereferencing of NULL pointer.:【下一篇】
【相关文章】
  • 初始化类成员小结(Effective C++条款12,13)
  • 为需要动态分配内存的类声明自己的拷贝构造函数和赋值操作符(Effective C++条款11)
  • Effective C++条款29: 避免返回内部数据的句柄
  • Effective C++条款47: 确保非局部静态对象在使用前被初始化
  • 基于EVC++ VS.net2005判断指定日期的星期又一法
  • VC++.NET winForm中怎样实现两个窗体互相修改对方TextBox的值,我是菜鸟:)
  • 学习C++我应该注意下什么?
  • C/C++发展历史简单介绍
  • C++/CLI的函数覆盖
  • C++ 读写MySQL经典
  • 【随机文章】
  • Vista Sidebar Gadget开发技巧几则
  • JavaScript 框架
  • 创建一个没有窗口的程序(2006-03-17修订)
  • Perl语言入门(第四版)--备忘录8
  • 模拟器
  • 驱动级的特征码修改——终级免杀之PcShare
  • 2003年最应该记住的网络照片
  • 技术支持程序员程序书写规范
  • 巧记双绞线压法
  • VP-40采集卡
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.