作者ytli1204 (YT)
看板Programming
标题[问题] 继承与多型的问题
时间Sun Apr 13 16:56:08 2008
※ [本文转录自 C_and_CPP 看板]
作者: ytli1204 (YT) 看板: C_and_CPP
标题: [问题] 继承与多型的问题
时间: Sun Apr 13 16:53:01 2008
现在要设计用於几何物件之间,计算两两相交情形的程式,
由於想要将物件放在一起管理,所以使用继承同一个 base class
的方式来设计几何物件,但是在计算相交的函式中,必须
要知道这两个物件的型别(ex: sphere to sphere),所以我
使用迂回的方式与重载来推导出物件的型别,但 compiler
会说重载无法找到符合的函式,请问要怎样写才会是对的?
或是架构应该要如何修改 @@
--------------------------
以下是我的程式架构..
我有一个 Base class : CGeometry
与两个Derived class : CSphere and CPoint
两个几何物件相交的回传值是 CIntersection
CGeometry 的定义 ==>
http://src.wtgstudio.com/?iw81Uv
CSphere 的定义与实作 ==>
.h
http://src.wtgstudio.com/?j3waI3
.cpp
http://src.wtgstudio.com/?0HQc58
CPoint 的定义与实作 ==>
.h
http://src.wtgstudio.com/?mqOS4R
.cpp
http://src.wtgstudio.com/?od5tJd
CIntersection 的定义 ==>
http://src.wtgstudio.com/?iPfY4P
将全部的东西放在一起管理成 GeometryLib.h
http://src.wtgstudio.com/?U52442
这是我测试用的 main.cpp ==>
http://src.wtgstudio.com/?5d0gI0
vc2003下的整个 project
http://0rz.tw/7c3TC
------------------------------------------------
测试结果 ==>
http://0rz.tw/ff3TO
由测试可以知道在非多型的情况下可以进入正确的 intersection 函式
在多型的情形下也会先进入 derived 的 intersection 函式
arr[0]->intersection(*arr[1]);
会印出 In point 代表是在 CPoint 内的 intersection 函式
也代表了在动态时可以推出 *this 是 CPoint 才对 @@
在这个情况下 我使用 rhs.intersection(*this) (注解的地方!!)
理论上应该 rhs会经由多型 得知自己是 CSphere
再经由重载 选出要执行 intersection(const CPoint&) 这个函式
可是在编译时却会出现
error C2663: 'CGeometry::intersection' :
3 overloads have no legal conversion for 'this' pointer
请问要怎要修改才会是正确的写法??
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.210.75
※ 编辑: ytli1204 来自: 140.113.210.75 (04/13 16:53)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.210.75