I want to use inheritance so that the object is handled differently, depending on where in the hierarchy it falls
( similar to this C # question )
Suppose you create a hierarchy of Shape objects, for example:
class Shape {} ; class Sphere : public Shape {} ; class Triangle : public Shape {} ; ...
Then you equip the Ray class with methods such as:
class Ray { Intersection intersects( const Sphere * s ) ; Intersection intersects( const Triangle * t ) ; };
You store an array of various shapes * of various types and call
vector<Shape*> shapes ; ...
But you get a compiler error
error C2664: 'Intersection Ray :: intersects (const Sphere *) const': cannot convert parameter 1 from 'Shape * const' to 'const Sphere *'
How are you wrong?
This is the only way to do it the other way,
class Shape { virtual Intersection intersects( const Ray* ray )=0 ; } ;
then does each class override the intersection? Then calls
//foreach shape.. Intersection int = shapes[i]->intersects( ray ) ;
Can you do this in the first way that I showed or NEVER?
bobobobo
source share