Kisser Leon

这个kisser不太冷
posts - 100, comments - 102, trackbacks - 0, articles - 0

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

Posted on 2007-03-09 10:36 kk 阅读(1318) 评论(5)  编辑 收藏 引用 所属分类: C++
第三个版本了!居然有搞错了。原本以为已经理解了多分派,写出来以后才知道问题那么多!所以说要多实践,多和高手讨论讨论阿。
如果还有问题,请大家不吝赐教。谢谢哈!
#include <iostream>
#include <list>
using namespace std;
//B
class B
{
};
class BE : public B
{
};
//A
class A
{
public:
 void virtual output(B * b){cout << "A:B" << endl;}
 void virtual output(BE * b){cout << "A:BE" << endl;}
};
class AD : public A
{
public:
 void output(B * b){cout << "AD:B" << endl;}
 void output(BE * b){cout << "AD:BE" << endl;}
};
int main()
{
 A * pA = new AD;
 pA->output(new BE);
 list<B*> * listb = new list<B*>();
 listb->push_back(new BE);
 pA->output(listb->back());
 
 return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
以下有问题!!
居然写错了!哈哈。谢谢各位大哥的评论阿。这个代码是我以前写的,我还以为是正确的。。。晕了。哈哈
下面的代码是改过以后的,不知道还有没有问题?希望各位多提意见哈
#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;
}

Feedback

# re: c++只支持单分派(single dispatch)  回复  更多评论   

2007-03-09 12:03 by xq
有错误多了一个 end:

我在linux下修改后试了,可以:

D1
E1

# re: c++只支持单分派(single dispatch)  回复  更多评论   

2007-03-09 12:06 by Navi
代码没有问题,是链接错误。

出现这个链接错误:
cannot open output file D:\Project\acm\test\Debug\test.exe: Permission denied
最有可能的原因是:test.exe仍然在后台运行。

# re: c++只支持单分派(single dispatch)  回复  更多评论   

2007-03-09 14:04 by Kooyu
这段代码没有任何问题,作者想演示C++不支持双分派的事实,但可惜,代码并没演示出来。

# re: c++只支持单分派(single dispatch)  回复  更多评论   

2007-03-11 15:59 by netdigger
print(int i)不是虚函数,而print()是虚函数。
基类里面没有声明printf(int i)怎么可能引用到?

# re: c++只支持单分派(single dispatch)  回复  更多评论   

2007-03-12 09:08 by 小熊
看来我还是理解有误阿
再研究研究哈
3q.

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理