Mail Archives: djgpp/1998/01/21/03:15:36
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 -