Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Why is RAII called RAII?

Reply
Thread Tools

Re: Why is RAII called RAII?

 
 
Goran Pusic
Guest
Posts: n/a
 
      09-15-2010
On Sep 15, 6:27*am, "Balog Pal" <(E-Mail Removed)> wrote:
> "Goran Pusic" <(E-Mail Removed)>
>
> >> Even the traditional 2-phase init has its good use cases, especially if
> >> you
> >> can chose it. See MFC's CFile. You have both ctor that opens file
> >> immediately from passed info and throws on error. And another that just
> >> creates the object and you can Open() later. And can reuse it too, after
> >> close.

> >Yes. My contention is: this is incidental complexity that serves not
> >enough purpose.

>
> C++ is traditionally multi-paradigm, so you are allowed to follow your
> attitude, but leave others to have their ways too.
>
> >E.g. why would you want to "reuse" CFile object?

>
> For example to close the log file every 24 (or 1 or whatever) hours and open
> another. *Or when it gets idle.


Well, for that, you can just create a new one without any measurable
performance hit. Look:

class whatever
{
std::auto_ptr<CFile> file;
CString name;
void reopen()
{
file = std::auto_ptr<new CFile(name)>;
}
};

Above beats e.g.

class whatever
{
CFile file;
CString name;
void reopen()
{
file.Close();
if (!file.Open(name))
handle error;
}
};

> >It's
> >infinitely more expensive to actually open a file on the file system
> >than to construct a C++ object. There has to be a rather particular
> >use-case (IMHO) to warrant two-phase construction.

>
> This is not multiphace contruction, but a separate use case. Or better put,
> mutable state of your object during its lifetime.
>
> Your logic could go out to the mutable/immutable class concepts. *Both are
> valuable, and have their good use. * Trying to talk down one because the
> other is useful hardly helps.
>
> >And indeed, there are stream class hierarchies in other frameworks
> >(VCL of Borland, Java, .NET), containing file-backed streams, that
> >have no separate "open", and I don't remember people complaining about
> >any issues.

>
> People are not complaining because whenever that interface is bad for their
> use approach they pick some other tool and move on.


OTOH, Java or .NET are not going away...

Goran.

 
Reply With Quote
 
 
 
 
ptyxs
Guest
Posts: n/a
 
      09-16-2010
On Sep 15, 9:51*am, Goran Pusic <(E-Mail Removed)> wrote:
> On Sep 15, 6:27*am, "Balog Pal" <(E-Mail Removed)> wrote:
>
>
>
> > "Goran Pusic" <(E-Mail Removed)>

>
> > >> Even the traditional 2-phase init has its good use cases, especially if
> > >> you
> > >> can chose it. See MFC's CFile. You have both ctor that opens file
> > >> immediately from passed info and throws on error. And another that just
> > >> creates the object and you can Open() later. And can reuse it too, after
> > >> close.
> > >Yes. My contention is: this is incidental complexity that serves not
> > >enough purpose.

>
> > C++ is traditionally multi-paradigm, so you are allowed to follow your
> > attitude, but leave others to have their ways too.

>
> > >E.g. why would you want to "reuse" CFile object?

>
> > For example to close the log file every 24 (or 1 or whatever) hours and open
> > another. *Or when it gets idle.

>
> Well, for that, you can just create a new one without any measurable
> performance hit. Look:
>
> class whatever
> {
> *std::auto_ptr<CFile> file;
> *CString name;
> *void reopen()
> *{
> * file = std::auto_ptr<new CFile(name)>;
> *}
>
> };
>
> Above beats e.g.
>
> class whatever
> {
> *CFile file;
> *CString name;
> *void reopen()
> *{
> * file.Close();
> * if (!file.Open(name))
> * *handle error;
> *}
>
>
>
> };
> > >It's
> > >infinitely more expensive to actually open a file on the file system
> > >than to construct a C++ object. There has to be a rather particular
> > >use-case (IMHO) to warrant two-phase construction.

>
> > This is not multiphace contruction, but a separate use case. Or better put,
> > mutable state of your object during its lifetime.

>
> > Your logic could go out to the mutable/immutable class concepts. *Both are
> > valuable, and have their good use. * Trying to talk down one because the
> > other is useful hardly helps.

>
> > >And indeed, there are stream class hierarchies in other frameworks
> > >(VCL of Borland, Java, .NET), containing file-backed streams, that
> > >have no separate "open", and I don't remember people complaining about
> > >any issues.

>
> > People are not complaining because whenever that interface is bad for their
> > use approach they pick some other tool and move on.

>
> OTOH, Java or .NET are not going away...
>
> Goran.


As I understand it "RAII", id est 'resource acquisition is
initialization" is quite simple and quite logical. It means : "In C++
you must acquire resources by initializing an object which
encapsulates these resources : as a consequence when the object goes
out of scope the resources are automatically released.
Extremely simple non ?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why is RAII called RAII? Johannes Schaub (litb) C++ 2 09-18-2010 09:18 AM
Re: Why is RAII called RAII? cpp4ever C++ 1 09-13-2010 06:25 AM
Why is a button Click event also called when a textbox TextChanged event is called??? S_K ASP .Net 6 11-08-2007 07:44 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments