Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!lll-tis!ames!ptsfa!pacbell!att-ih!occrsh!erc3ba!rsc From: rsc@erc3ba.UUCP (Rich Chomiczewski, AT&T - ERC, Princeton NJ) Newsgroups: comp.lang.c++ Subject: Following code bombs (possible assignment/initialization problem)? Message-ID: <400@erc3ba.UUCP> Date: 4 Mar 88 18:21:42 GMT Organization: AT&T ERC, Princeton NJ Lines: 54 Keywords: c++ X(X&) operator=(X&) The following program blows the stack: // --------------------- Cut Here -------------------- #include class Point { int xc, yc; // x-y coordinates public: Point(int x =0, int y =0) { xc = x; yc = y; } int x() { return xc; } int y() { return yc; } Point operator+(Point p) { return Point(xc+p.xc, yc+p.yc); } }; ostream& operator<<(ostream& oo, Point p) { return oo << "(" << p.x() << "," << p.y() << ")"; } class Position { Point off; // offset is relative to (possibly null) ``to''. Position *to; public: Position() { off = Point(0,0); to = 0; } Position(Point p) { off = p; to = 0; } Position(Point p, Position pos) { off = p; to = &pos; } Point position() { return (to) ? off+to->position() : off; } }; ostream& operator<<(ostream& oo, Position p) { return oo << p.position(); } main() { Position a = Position(Point(1,1)); Position b = Position(Point(5,5), a); Position c = Position(Point(10,10), b); cerr << a; cerr << b; cerr << c; } // --------------------- Cut Here -------------------- One of the more difficult aspects of programming in c++ for me is the correct handling of assignment and initialization of classes (which I suspect is the root of the problem here). So, given the above declaration of class Position what is the "correct" definition of the member functions ``Position::Position(Position&)'' and ``Position::operator=(Position&)''? Thanks, Rich Chomiczewski (ihnp4!erc3ba!rsc)