Hi Rick,
Each time inside the loop, dir object will be created and destroyed.
The first instance will not stay around. It will be destroyed when the first
iteration of the for loop completes.
The dir object is statically created inside the for loop and the life time
and scope of such objects will be local to the block where it is created.
These objects generally gets created on the stack and will be destroyed
automatically when it goes out of scope.
The destructor will be called properly during such destruction.
In this case for each iteration of the for loop, the constructor and
destructor of dir object will be called.

I feel this is not the best way of doing it. There are lot of other better
ways to do that.
If i am not wrong, i would say that the way dir object is modelled was
wrong.
Instead of creating object every time, it would rather can be created once.
It should expose a public interface for processing different
directory/files.
There are lot of other smart people in this group. I believe they can give
you suggestions on better way of designing it.

Thanks,
Thanga



On Fri, Oct 2, 2009 at 8:42 PM, Rick <[email protected]> wrote:

>
>
> Hi all,
>
> I have a class, called Directory, that
> opens/closes/reads/writes/displays/etc file directories. I use it in
> an application that allows me to search for information, and the user
> can search multiple directories for information.
>
> Here is a small snipet of code that I use:
>
> for (int iy = 0; iy < fileLevel; ++iy)
> {
> Directory dir(dirname, fileNames[iy]);
> dir.process();
> }
>
> My question is, since I define "Directory dir(dirname,
> fileNames[iy]);" within my loop, what happens with
> construction/destruction of the "dir" object? Will the first instance
> stay around and be overwritten by the next? Will it's memory become
> deallocated? Will the destructor run before creating the next instance?
>
> Lastly, is there a better way to do this? An array of Directory objects,
> maybe?
>
> Thank you,
>
> ~Rick
>
>  
>


[Non-text portions of this message have been removed]

Reply via email to