delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/21/03:15:36

Newsgroups: comp.os.msdos.djgpp,comp.os.os2.programmer.misc,gnu.gcc.help
From: mveksler AT techunix DOT technion DOT ac DOT il (Veksler Michael)
Subject: Re: C++ problem -- Over 30 people were unable to help me, can you?!
Organization: Technion, Israel Institute of Technology
Date: Wed, 21 Jan 1998 07:50:11 GMT
Message-ID: <En4J3n.6F9@discus.technion.ac.il>
Followup-To: comp.os.msdos.djgpp,comp.os.os2.programmer.misc,gnu.gcc.help
References: <bWLoegW7sFse-pn2-d679pGp6mnys AT portD23 DOT Generation DOT NET>
Sender: news AT discus DOT technion DOT ac DOT il (News system)
Lines: 69
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gili (NOSPAMsl AT psycode DOT com) wrote:
: Hi,

: 	I created a class called File() which inherites fstream() publically 
: (i.e. class File : public fstream)
: I'd like to call fstream()'s constructor whenever File()'s constructor
: is called. However, I have run into a major problem.
: I can call the constructor in this way:
: "
: File::File(const char* name, int mode, int prot):
: fstream(name, mode, prot)
: {
:   <body goes here>
: }
: "

: 	However, I'd like to use exception-handling and that isn't possible 
: in the constructor initialization. What I'd like to do is to be able

That is wrong, the C++ draft states that it is possible to have try
catch block for the constructor initializer.
Quote from the working document:
>> 3 A  function-try-block  associates a handler-seq with the ctor-initial-
>>   izer, if present, and the function-body.  An exception  thrown  during
>>   the  execution  of the initializer expressions in the ctor-initializer
>>   or during the execution of the function-body transfers  control  to  a
>>   handler in a function-try-block in the same way as an exception thrown
>>   during the execution of a try-block transfers control  to  other  han-
>>   dlers.

And the example they give is:

>>   C::C(int ii, double id)
>>   try
>> 	     : i(f(ii)), d(id)
>>   {
>> 	     // constructor function body
>>   }
>>   catch (...)
>>   {
>> 	     // handles exceptions thrown from the ctor-initializer
>> 	     // and from the constructor function body
>>   }
>>

Unfortunately I suspect gcc does not recognize this (does it ?).
If it does not, you should try egcs. (I suspect that DOS egcs version
is not working yet, but you should check it anyway).

: to call the fstream() constructor from the body of File()'s 
: constructor. This would allow me to use exception-handling... However,
: simply calling fstream() in the body seems to make C++ think I'm 
: creating an fstream() variable instead of calling the constructor. 
: Also, when I try using ::fstream() to call the constructor, the call 
: *seems* to work but my file does not open correctly (which it does 
: when fstream() is called from the constructor initialization.) Does 
: anyone have any idea how this problem could be solved?

This is plain wrong, when you try to initialize your fstream *INSIDE* the
constructor, it already got initialized using a default constructor.
There must be a way to get the same results by correct fstream::
member function calls.

: Gili


Hope this helps

  Michael

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019