Hi there, I have written a simple program which I know a solution to, I simply don't understand why the version below doesn't work. I have investigated the answer to roughly how it goes wrong, I just don't know why! Where the problems begin is commented in the last six lines of code, and it's frustrating as I believe it is pretty elegant this way... only it does work. More remarks follow the listing:
#include <iostream>
class Point
{
private:
int *itsX;
int *itsY;
public:
Point();
Point(int x, int y);
~Point();
void SetX(int x);
void SetY(int y);
void SetXY(int x, int y);
int GetX() const;
int GetY() const;
};
Point::Point()
{
itsX = new int(0);
itsY = new int(0);
}
Point::Point(int x, int y)
{
itsX = new int(x);
itsY = new int(y);
}
Point::~Point()
{
delete itsX;
delete itsY;
}
void Point::SetX(int x)
{
*itsX = x;
}
void Point::SetY(int y)
{
*itsY = y;
}
void Point::SetXY(int x, int y)
{
*itsX = x;
*itsY = y;
}
int Point::GetX() const
{
return *itsX;
}
int Point::GetY() const
{
return *itsY;
}
class Rectangle
{
private:
Point *itsLowerLeft;
Point *itsUpperRight;
public:
Rectangle();
Rectangle(int llx, int lly, int urx, int ury);
~Rectangle();
void SetLowerLeft(int x, int y);
void SetUpperRight(int x, int y);
Point GetLowerLeft() const;
Point GetUpperRight() const;
int GetArea() const;
};
Rectangle::Rectangle()
{
itsLowerLeft = new Point(0, 0);
itsUpperRight = new Point(1, 1);
}
Rectangle::Rectangle(int llx, int lly, int urx, int ury)
{
itsLowerLeft = new Point(llx, lly);
itsUpperRight = new Point(urx, ury);
}
Rectangle::~Rectangle()
{
delete itsLowerLeft;
delete itsUpperRight;
}
void Rectangle::SetLowerLeft(int x, int y)
{
itsLowerLeft->SetXY(x, y);
}
void Rectangle::SetUpperRight(int x, int y)
{
itsUpperRight->SetXY(x, y);
}
Point Rectangle::GetLowerLeft() const
{
return *itsLowerLeft;
}
Point Rectangle::GetUpperRight() const
{
return *itsUpperRight;
}
int Rectangle::GetArea() const
{
return(itsUpperRight->GetX() - itsLowerLeft->GetX()) *
(itsUpperRight->GetY() - itsLowerLeft->GetY());
}
int main(int argc, char *argv[])
{
Point origin(0,0);
std::cout << "origin.x = " << origin.GetX() << std::endl;
std::cout << "origin.y = " << origin.GetY() << std::endl;
Point *pOrigin = new Point(0, 0);
std::cout << "pOrigin->x = " << pOrigin->GetX() << std::endl;
std::cout << "pOrigin->y = " << pOrigin->GetY() << std::endl;
Rectangle square(0, 0, 1, 1);
//un-comment line below to cause crash:
//std::cout << "square llx = " << square.GetLowerLeft().GetX() << std::endl;
Rectangle *pSquare = new Rectangle(0, 0, 1, 1 );
//un-comment both lines below to cause crash:
//std::cout << "pSquare llx = " << pSquare->GetLowerLeft().GetX() << std::endl;
//std::cout << "pSquare llx = " << pSquare->GetLowerLeft().GetX() << std::endl;
}
When I threw the problem up for grabs a programmer at Lionhead (TheBag) pointed out that if I alter my Get functions in the rectangle class so they return a pointer to each corner I can make it work. He's right. I only don't understand why, when I try to return the Point object, as I do above, the program tends to crash. Why is this illegal, particularly as it compiles ok?
All I can tell is that it seems to call the Point destructor After (for some reason) it has completed each cout statement. I know this as I have tried putting couts in every separate function in the program to see where it's at. If I remove the deletes in the Point destructor it carries on without crashing, but seems silly as I'm supposed to be deleting my pointers in my destructors, so the question is, please tell me why my program doesn't work when I remove the slashes from the comments above?