Alexander
Mon Dec 15 14:19:14 CST 2003
You are absolutely right. One can't catch the exceptions in the
constructors of its base classes within ones constructor. And the
original code was instantiating a temporary CFile object. However,
I don't consider myself a C++ guru...
--=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ:
http://www.mvps.org/vcfaq
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
"sbogus" <sbogus6@hotmail.com> wrote in message =
news:OKdmCoxwDHA.2396@TK2MSFTNGP09.phx.gbl...
> Hi Bonj,
> your code is probably wrong. Use this piece instead...
>=20
> CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename) throw
> (CFileException)
> : CFile(lpszFilename,CFile::modeCreate | CFile::modeWrite |
> CFile::typeBinary)
> {
> // You don't need a try...catch block here, since if there's =
something
> wrong, the constructor will throw the exception,
> // or one of the base class constructors will do that work for =
you.
> if(m_hFile !=3D hFileNull)
> OpenedOK =3D true; // this should occur
> else
> {
> OpenedOK =3D false;
> throw new CFileException();
> }
> }
>=20
> May be gurus of c++ language will condemn my "confusity", but I think =
the
> explicit call of the constructor method you make in your inherited =
class
> does not do what you want to do - executing code from your constructor =
means
> all the constructors of the base classes are called (in this case the
> default constructors) and in particular, your m_hFile member is really
> NULL-ed by the CFile's default constructor. Calling the CFile =
constructor
> will in my opinion initialize a temporary object only and will not
> initialize the inherited data members. May be I'm wrong... Anyway, the =
above
> piece of code will work for you.
>=20
> Hope to helped to you.
>=20
> Kind regards,
> sbogus_confused
>=20
>=20
>=20
> "terry" <back@ya.com> schrieb im Newsbeitrag
> news:brj2vp$1kt$1@news7.svr.pol.co.uk...
> > Shouldnt u be instantiating m_hFile somewhere. It should either be
> returned
> > by the file create function or passed as a parameter to the file =
create
> > function.
> >
> > T
> > "Bonj" <anonymous@discussions.microsoft.com> wrote in message
> > news:01ba01c3c2a5$4c94d300$a401280a@phx.gbl...
> > > Hi
> > > I've got the following code for the constructor of a
> > > class that I've derived from CFile. It is in a project
> > > that I've created using ATL COM AppWizard, with MFC
> > > support. So it's using ATL *and* MFC.
> > > However when I try to write to the file, it asserts in
> > > the first line of the CFile::Write class, on the line:
> > > ASSERT (m_hFile !=3D hFileNull)
> > >
> > > so I decided to put this test in my own class so I could
> > > check for it - my code is as follows:
> > >
> > > CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename)
> > > {
> > > try
> > > {
> > > CFile::CFile(lpszFilename,
> > > CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
> > > if(m_hFile !=3D hFileNull)
> > > OpenedOK =3D true;
> > > else
> > > OpenedOK =3D false; //<-this occurs.
> > > }
> > > catch(CFileException e)
> > > {
> > > afxDump << "File could not be opened " <<
> > > e.m_cause << "\n";
> > > OpenedOK =3D false;
> > > }
> > >
> > > }
> > >
> > > However I checked in windows explorer and the file IS
> > > being created. How can I get the file handle to be non-
> > > null, and thus enable writing to the file? thanks
> >
> >
>=20
>