如果我有一个类Rectangle
初始化对象有和没有新的运营商
class Rectangle{
private:
double width;
double height;
public:
void Set(double w , double l){
width = w;
height = l;
}
};
我decleare对象,例如:
Rectangle *Obj;
,然后尝试初始化它的属性:
Obj->Set(3,5);
编译器在运行时显示:The variable 'Obj' is being used without being initialized.
的问题可以通过解决:
Rectangle *Obj=new Rectangle;
我想请教一下原因!为什么编译器在编译时不显示任何错误?
只是一个nit,但是'Rectangle * Obj;'不只是_declare_;它也是_defines_。它确实保留了内存;足够的内存来存储指针。 –
@JamesKanze:你当然是对的。我会马上更新我的回答 – arne