Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!samsung!emory!ogicse!plains!gus From: gus@plains.UUCP (jim gustafson) Newsgroups: comp.lang.pascal Subject: Re: (one way to) Copying objects in Turbo Pascal 5.5 Message-ID: <4878@plains.UUCP> Date: 25 May 90 08:09:15 GMT Organization: North Dakota State University Fargo, ND Lines: 57 >I'd like to have a constructor for an object that just copies another instance >of it or one of its descendants, and (the hard part) also copies the type >information. Here's one way to get part of what you want. (the 'new' constructor could have been 'Init') ----------------------------------------------- PROGRAM TestCopy(Input, Output); TYPE ClassTest = OBJECT a, b : CHAR; CONSTRUCTOR new; CONSTRUCTOR copy(VAR oldInstance:ClassTest); PROCEDURE print; END; ClassTest2 = OBJECT(ClassTest) x, y : INTEGER; CONSTRUCTOR new; PROCEDURE print END; CONSTRUCTOR ClassTest.new; BEGIN a:= 'a'; b:= 'b' END; CONSTRUCTOR ClassTest.copy(VAR oldInstance:ClassTest); BEGIN self:= oldInstance END; PROCEDURE ClassTest.print; BEGIN Writeln('ClassTest.a is ''', a, ''' ClassTest.b is ''', b,'''') END; CONSTRUCTOR ClassTest2.new; BEGIN x:= 0; y:= 1 END; PROCEDURE ClassTest2.print; BEGIN Writeln('ClassTest2.x is ''', x, ''' ClassTest2.y is ''', y, '''') END; VAR aTest2, aCopyOf2 : ClassTest2; BEGIN aTest2.new; aTest2.print; aCopyOf2.copy(aTest2); aCopyOf2.print; END. -- Jim Gustafson UUCP: uunet!plains!gus North Dakota State University Bitnet: gus@plains Fargo, North Dakota 58105 Internet: gus@plains.nodak.edu