Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!ut-emx!jamshid From: jamshid@ut-emx.uucp (Jamshid Afshar) Newsgroups: comp.lang.c++ Subject: Re: BC++ and String class?? Summary: use constructor's member initialzer list Keywords: member initialization constructor Message-ID: <48885@ut-emx.uucp> Date: 13 May 91 01:08:50 GMT References: <91131.195732ACPS2924@Ryerson.Ca> Organization: The University of Texas at Austin; Austin, Texas Lines: 88 Hey, a BC++ question that actually belongs in this group. In article <91131.195732ACPS2924@Ryerson.Ca> ACPS2924@Ryerson.Ca writes: >I'm having a slight problem with BC++. Following is a short code sample >from my header file. I somehow keep getting the same error with String. >What am I doing wrong ?? > >//-------------------------------------------------------------------------- > >...some #includes deleted... >#ifndef __STRNG_H >#include >#endif > class String { // defined in strng.h public: String(const char*); String(const String&); ...}; > >class dbField { >public: > dbField(String name) > { fieldName = name; }; >private: > String fieldName; >}; > >ERROR: Cannot find 'String::String()' to initialize field 'fieldname' .... The dbField constructor should be: dbField(String name) : fieldName(name) // this is the member initialization list { } Remember that all the data members of a class object are constructed _before_ entering the body of the constructor. If you do not explicitly call a data member's constructor using the member init. list, its default constructor will be called (which String lacks, hence the error). Besides, it's good style to initialize data members using the init. list (it's mandatory if they're const) as opposed to using assignment in the construtor body. Almost all my constructors have nothing between the curly braces. While I'm dictating style, the constructor should really be 'dbField(const String& name)'. Use |const| because you're not changing 'name'. Pass by reference so your program doesn't have to make a temporary String object and so that it won't 'clip' objects that are of a type derived from String. I would guess 95% of the parameters in my code are |const Type&|. >But in the example directry.cpp it somehow gets away with it. Why ??????? Check it again. The Directory class constructor does initialize its String member using String(const char*). >Peter >acps2924@ryerson.ca ! Who is the ! If plumbers designed toilets like > ! user,pray, ! software professionals design tools, > ! and who is ! we'd be up to our knees in crap. > ! the used ? ! - Charles A. Rovira To all: when posting code that does not compile, please indicate what compiler and version you are using, the exact error text, and mark the line of code that gave the error. Also, if the code references a type or a macro, please include their (abbreviated) definitions. BTW, I know it's a style thing, but I find those #ifndef wrappers around includes really annoying. They don't have enough of an effect on compilation speed to warrant their use. This is especially true for Borland C++ since you can use pre-compiled headers. Actually, they are implemented kinda weird (of course, I don't know how others do it). BC++ don't 'compile' a .h file, it saves the effects a series of '#include' statements and uses it when it sees the same series of '#include's (and all the compiler options are the same). Finally, a couple of plugs: To subscribe to the Turbo(Borland) C++ mailing list, send e-mail to listserv@ucf1vm.cc.ucf.edu or listserv@ucf1vm.bitnet containing the line subscribe tcplus-l Jane Doe There's a BC++ bug list at sun.soe.clarkson.edu [128.153.12.3] in ~ftp/pub/Turbo-C++/bug-report and by e-mail: To: archive-server@sun.soe.clarkson.edu Subject: send pub/Turbo-C++/bug-report Jamshid Afshar jamshid@emx.utexas.edu