指针数组,数组里面每个元素指向一个对象
一个指针数组,数组里面每个元素指向一个对象shape,然后就出错了,please help me !!!
class shape{
protected:
double xCoord,yCoord;
public:
shape(double x,double y) : xCoord(x),yCoord(y){}
virtual double area() const{return 0.0;}
};//.............................
class Triangle{
protected:
shape* p[3];
public:
Triangle();
double area() const;
};//.................................
Triangle::Triangle(){
*p=new shape(0,0); //这里有错,下同
p++;
*p=new shape (3,0);
p++;
*p=new shape(3,4);
}
double Triangle::area() const {
return 1.1;
}//................................
[解决办法]
Triangle::Triangle()
{
*p = new shape(0,0); //这里有错,下同
*(p+1) = new shape (3,0);
*(p+2) = new shape(3,4);
}
class shape{
protected:
double xCoord,yCoord;
public:
shape(double x,double y) : xCoord(x),yCoord(y){}
virtual double area() const{return 0.0;}
};//.............................
class Triangle{
protected:
shape* p[3];
public:
Triangle();
double area() const;
};//.................................
Triangle::Triangle(){
p[0]=new shape(0,0);
p[1]=new shape (3,0);
p[2]=new shape(3,4);
}
double Triangle::area() const {
return 1.1;
}//................................