delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/21/16:46:12

From: Andrew killer of Spam <pitonyak AT bigfoot DOT com>
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: <bWLoegW7sFse-pn2-d679pGp6mnys AT portD23 DOT Generation DOT NET> <En4J3n DOT 6F9 AT discus DOT technion DOT ac DOT il>
Reply-To: pitonyak AT bigfoot DOT com
NNTP-Posting-Host: 38.227.230.18
Mime-Version: 1.0
CC: sl AT psycode DOT com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

> :       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 -


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