Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!mips!sgi!shinobu!odin!sgihub!dragon!xanadu.wpd.sgi.com!pal From: pal@xanadu.wpd.sgi.com (Anil Pal) Newsgroups: comp.lang.c++ Subject: Re: Need `set before used' warning Keywords: c++, warning Message-ID: <1990Sep27.001259.25809@relay.wpd.sgi.com> Date: 27 Sep 90 00:12:59 GMT References: <2@mtnmath.UUCP> Sender: news@relay.wpd.sgi.com ( CNews Account ) Reply-To: pal@sgi.com Organization: Silicon Graphics, Inc. Lines: 26 In article <2@mtnmath.UUCP>, paul@mtnmath.UUCP (paul) writes: |> class Example { |> char * Point ; |> public: |> Example(char * point) {point=Point;} // NOTE inverted intialization! |> }; One helpful hint to avoid this problem is to initialize the variables the way you would call a base class constructor, rather than in the body of the constructor. Thus class Example { char * Point ; public: Example(char * point) : Point(point) {} }; With this syntax, interchanging the two will cause an error at compile time. As a subtle point, this also *initializesa* Point, rather than *assigning* to it. This is irrelevant for a char*, but makes a difference for consts, references, and user-defined classes where initialization and operator= are different. Anil A. Pal, Silicon Graphics, Inc. pal@sgi.com (415)-335-7279