Mail Archives: djgpp/1998/01/21/16:46:12
> : 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.
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
- Raw text -