c++只支持单分派(single dispatch)
2007-01-09 18:30
302 查看
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;
}
该程序build信息如下:
--------------------Configuration: test - Debug--------------------
Compiling source file(s)...
test.cpp
test.cpp: In function `int main()':
test.cpp:60: warning: label `end' defined but not used
test.cpp:64:2: warning: no newline at end of file
Linking...
C:/MinGWStudio/MinGW/bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/bin/ld.exe: cannot open output file D:/Project/acm/test/Debug/test.exe: Permission denied
collect2: ld returned 1 exit status
test.exe - 1 error(s), 2 warning(s)
#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;
}
该程序build信息如下:
--------------------Configuration: test - Debug--------------------
Compiling source file(s)...
test.cpp
test.cpp: In function `int main()':
test.cpp:60: warning: label `end' defined but not used
test.cpp:64:2: warning: no newline at end of file
Linking...
C:/MinGWStudio/MinGW/bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/bin/ld.exe: cannot open output file D:/Project/acm/test/Debug/test.exe: Permission denied
collect2: ld returned 1 exit status
test.exe - 1 error(s), 2 warning(s)
相关文章推荐
- 再次探讨c++只支持single dispatch
- Double Dispatch(双分派)(c++ 版)
- error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
- C++ 9.2迭代器支持的运算和迭代器范围
- 阿里云SDK再升级,宣布支持C++语言
- C++ 9.3.4 容器支持的运算---关系操作符
- Single Number c++
- C语言对文件操作的支持(C++可用)
- C++ 支持中文标识符?
- 类模板相互引用的问题(错误:缺少类型说明符-假定为int。注意:C++不支持默认int)
- 15.[个人]C++线程入门到进阶(15)----线程函数:WaitForSingleObject
- windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)
- error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
- C++ Language Standards Supported by GCC ( GCC 支持的C++语言标准 )
- C++为什么不支持某些东西
- C++支持的RTTI
- Netbeans 安装和配置 C/C++ 支持
- C++ cin不支持录入空格
- 测试一下 Intel C++8.0 对模板的支持程度zz