mercredi 20 juillet 2016

Initializing an object with and without new operator

If I have a class Rectangle

class Rectangle{

private:
    double width;
    double height;


public:
void    Set(double w , double l){
    width   = w;
    height  = l;
}
};

and I decleare an object such:

Rectangle *Obj;

and then try to initialize its properties:

Obj->Set(3,5);

the compiler shows at run-time: The variable 'Obj' is being used without being initialized.

The problem can be solved by:

Rectangle *Obj=new Rectangle;

I would ask about the reason! And why the compiler doesn't show any error at compiling time?

Aucun commentaire:

Enregistrer un commentaire