From: Andrew killer of Spam Newsgroups: comp.os.msdos.djgpp,comp.os.os2.programmer.misc,gnu.gcc.help Subject: Re: C++ problem -- Over 30 people were unable to help me, can you?! Date: Wed, 21 Jan 1998 11:37:55 -0500 Organization: Spam Killer Inc. Lines: 60 Message-ID: <34C62463.2B06@bigfoot.com> References: Reply-To: pitonyak AT bigfoot DOT com NNTP-Posting-Host: 38.227.230.18 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit CC: sl AT psycode DOT com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk > : 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) > : { > : > : } > : " > > : 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. OK, so no compiler currently support what you really want to do, or at least not the compiler that you use. A possible solution that I am not going to completely explore at the moment would be to make the constructors on your class protected, and then use another class to create them. I have seen things such as this done before for reasons very similar to this. class MyStream : public fstream { protected: // ** all constructors go here // ** declare class UseMe as a friend } class UseMe { public: //** Constructors go here which encapsulate the creation //** of MyStream, complete with exception handeling //** Depending on the use, you can then have cast operators //** so that this can be used where an fstream is needed. //** You can also create all of the same functions and procedures //** so that this can be used the same was as the MyStream. private: MyStream* _theStream; } Yes, it is ugly and perhaps not what you want, but if you use smart pointers, you could potentially even NOT really delete _theStream in the destructor of UseMe and then simply use _theStream as you desire... Andrew D. Pitonyak SPAM_ME_AND_DIEandyp AT primatech DOT com Remove SPAM_ME_AND_DIE to send me e-mail directly. the bigfoot address in the header should work as well