Reputation: 17
I am building a simple package manager for my new Linux distro (check it out as soon as I finish) and have run into a problem. My install()
and remove()
functions need to access an unspecified amount of Package
objects. This is the install function.
void install(int argc, char *argv[]) throw()
{
for (int i = 2; i <= argc; i++)
{
Package p(argv[i]);
p.fetch();
cout << "Package " << p.getRef() << " retrieved succesfully from server.\n";
p.install();
cout << "Package " << p.getRef() << " installed succesfully.\n";
}
}
I suspect this code isn't entirely kosher; this would result in several objects named p
which would probably cause a compilation and/or runtime error. What I'm looking for is either a way to change the name p
in each iteration of the loop or delete p
after one iteration so I will be free to create another object of that name.
Upvotes: 0
Views: 786
Reputation: 20063
There is no discernible problem with your code nor will there be any issue with multiple objects. The variable p
has automatic storage duration and is destroyed when it goes out of scope. When this means is that at the end of each iteration of the for
loop p
is destroyed and no longer exists.
Upvotes: 1