Newsgroups: comp.os.msdos.djgpp From: "Mike Ruskai" Message-ID: References: <5zeY1.843$SI2 DOT 270 AT news DOT cwix DOT com> <3631E505 DOT 4773CBD3 AT montana DOT com> X-Newsreader: PMINews 2.00.1201 For OS/2 Organization: TLF MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Subject: Re: construction has been delayed due to somebody's stupidity (most likely my own) Lines: 119 Date: Sat, 24 Oct 1998 20:36:52 GMT NNTP-Posting-Host: 24.3.130.120 NNTP-Posting-Date: Sat, 24 Oct 1998 13:36:52 PDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Sat, 24 Oct 1998 08:32:37 -0600, bowman wrote: > > >Mike Ruskai wrote: >> >> Well, the first thing I have to say is that your example does not demonstrate >> the problem you indicate when I try to compile it, with no less than four >> compilers (IBM VisualAge C++, Borland C++, GNU GCC, and DJGPP). > >yeah, but try the non-trivial snippet for interesting errors: > ><------------------------- badcode.cpp ----------------------> >#include > >class MyClass { >public: > MyClass(); > void say_hi(void); >protected: > int data; >}; > >void MyClass::say_hi(void) { > cout << "hello world\n"; >} > > >int main(void) >{ > MyClass instance(); // point of interest > instance.say_hi(); >} > ><--------------------------- end: badcode.cpp >-----------------------> This is bad code indeed, for you are declaring but not defining the default constructor. In addition, you're not declaring the variable correctly. If you're using the default constructor (which means the constructor that takes no arguments, whether or not you declared and defined it), you must not use empty parentheses. I wasn't sure about that, but I've since verified it. When you use empty parentheses, what you're actually doing is declaring a function called "instance" that returns a MyClass object. >or, for another variation on the theme, > ><--------------------------- stillbad.cpp >-----------------------------> > >#include > >class MyClass { >public: > MyClass(); > void say_hi(void); >protected: > int data; >}; > >void MyClass::say_hi(void) { > cout << "hello world\n"; >} > >int main(void) >{ > MyClass instance; // point of interest > instance.say_hi(); >} > ><------------------------------ end: stillbad.cpp >--------------------------> This is bad solely because you're declaring a constructor without defining it. Your linker should give you an unresolved external error. >this one actually compiles and executes under gcc. I don't know if the >proir two will >make it through any compiler, but I would be suspicious of a compiler >that did accept them. > ><------------------------------ end: thisworks.cpp >--------------------------> > > >#include > >class MyClass { >public: > MyClass() {}; > void say_hi(void); >protected: > int data; >}; > >void MyClass::say_hi(void) { > cout << "hello world\n"; >} > >int main(void) >{ > MyClass instance(); // point of interest > instance.say_hi(); >} > ><------------------------------ end: thisworks.cpp >--------------------------> This is bad because you're declaring a function instead of defining a variable. -- - Mike Remove 'spambegone' to send e-mail.