I’m working on an opengl project and I got a problem with some of my classes:
There are 3 main classes:
class scene{
public:
scene();
object** elements;
unsigned int nb_elements;
void add_object(object* _element){
objet** p_elements=elements;
while(p_elements-elements!=nb_elements)
p_elements++;
*p_elements=_element; //here is the bug
nb_elements++;
}
}
[...]
};
class object{
public:
object();
[...]
};
class point : public object{
point();
point(double _x, double _y, double _z,double _r);
[...]
}
I initialize the scene after initializing Opengl, in the same function:
void GLWidget::initializeGL(){
[OpenGL initialization]
_scene=new scene;
point* s1=new point(0.0,0.0,3.0,1.0);
_scene->add_object(s1);
}
The bug is in the scene::add_object function. I don’t understand why it doesn’t work since *p_elements and _element are both pointers. I gues it’s a cast problem but I don’t know how to fix it. I tried dynamic cast: the bug remains.
Thank you for your help,
Kamouth
objet** p_elements=elements;setsp_elementsto uninitialized memory. Sop_elements-elements!=nb_elements,p_elements++;and*p_elements=_element;invoke undefined behavior. I’m not going to advise you to initialize the memory, but merely to usestd::vectorand smart pointers instead.The double pointers suggest you come from a C background, but if you want to do C++ development you have to learn C++ idioms. Don’t stick to writing C code in C++.