Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!geac!alias!rae From: rae@alias.com (Reid Ellis) Subject: constructors and initializers Message-ID: <1991May8.155352.18313@alias.com> Keywords: constructor, default, class, array, definition Sender: news@alias.com (USENET News) Organization: Alias Research, Inc. Toronto ON Canada References: <1991May2.165955.28385@iitmax.iit.edu> <701@taumet.com> Date: Wed, 8 May 91 15:53:52 GMT A note about initializers in constructors. Recently, the following class was mentioned under the subject line "HELP: default constructor for array of class": class Point { int x; public: Point() { x = 200; } Point(int xc = 100) { x = xc; } }; Not to belabor the point [pun intentional] but the constructors should be written as: class Point { int x; public: Point() : x(200) {} Point(int xc = 100) : x(xc) {} }; i.e. use initializers rather than assignment in a constructor. Why? The following line of code won't work with the first version, but will work with the second version: const Point kPoint(23); I just thought I'd mention this since I see assignment in constructors in several examples people post here [not just "Point" :-)]. If this is imperious and sounds like I'm pontificating, feel free to send me mail saying so. :) Reid, a.k.a. "Mister nit pick" -- Reid Ellis rae@utcs.toronto.edu || rae@alias.com CDA0610@applelink.apple.com || +1 416 362 9181 [work]