[C++-sig] pointers and pyste

Sameer Agarwal sagarwal at cs.ucsd.edu
Sun Apr 27 22:13:10 CEST 2003


okay, here goes.

The class definitions are as follows

class Object{
  public:
   virtual int intersect (Ray & r)=0;
   Shader * mat;
};


class Ray{
  public:
   Vec3 origin;
   Vec3 direction;

   Object * hit_object;
   Ray(const Vec3 & o, const Vec3 & d );
   Ray(const Ray & r);
   void trace(Color & c, Scene & scene);
};


class Scene{
  public:
   Scene();
   Scene(int m, const Color & c);
   void builder();
   void render();
   void render(int xb, int xe, int yb, int ye);

   void add_object(Object & o);
   void add_light(Light & l);
   void add_camera(Camera & camera);
   void add_image(Image & image);
   void add_environment(Shader & env);

   std::list<Object*> object_list;
   std::list<Light*> light_list;
};


void Scene::add_object(Object & o)
{
   object_list.push_back(&o);
};


class Sphere: public Object {
  public:
   Vec3 position;
   breal radius;
   Sphere(const Vec3 & p, const breal & rr, Shader & mat);
   int intersect (Ray & r);
};


The trouble starts here.


void Ray :: trace(Color &c , Scene &scn )
{

   hit=0;
   if (level < scn.max_level)
     {
       std::list<Object*>::iterator o;
       o = scn.object_list.begin();


       while(o!=scn.object_list.end())
	{
/********************************
//troubled code

	  int retvalue = (*o)->intersect(*this);
***********************/

	o++;
	};
     };
   if(!shadow)
     {
       if (hit)
	(hit_object->mat)->shade(*this ,c, scn);
       else
       	scn.environment->shade(*this,c,scn);
     }
}

In the above function I called the virtual method intersect for each of 
the objects in the list object_list and pass it a reference to the 
class calling the method.
when I use boost to wrap the sphere class, the ray which is passed to 
Sphere::intersect has a different address than the one which actually 
calls the routine.
which I am guess is because somewhere along the way instead of a 
reference a copy of the object is being passed.
The problem is fixed if I explicitly exclude Sphere::intersect from the 
wrapper.

I hope this makes thing clearer.
thanks,
sameer





More information about the Cplusplus-sig mailing list