Click to See Complete Forum and Search --> : A Quick C++ Question


MrPotatoes
June 25th, 2008, 08:56 AM
i have a C++ background but i ended up doing web dev and did some Java but mostly PHP. i found there are a lot of nice things about the languages but overall i wanted performance and power so i came back and have been trying to write myself a small dispatch library as i reteach myself the language.

excuse the redundant use of the this *. it's easier to read imo and i don't forget what member property i'm using.

the question i have right now is about the new operator. i wanted to pass just "new $CLASS_NAME" as a param and then handle it within that function from there. here are some code snippets that i have (it's actually spread out alot so i'll do just the snippets unless asked)


// this is a function within the class:
...
RegisterController("blogs", new BlogsController);
...

// this is the actual function
void RegisterController(string Section, BlogsController * objRegistree)
{
this->Registrar[Section] = objRegistree;
}

// This is the destructor
~ControllerRegistrar()
{
// iterate thru this and delete them all
delete this->Registrar["blogs"];
// this is temporary. didn't write the loop code yet
}


my question is what do i need to make sure i do to not lose that memory if i can do this. it seems to work just file on compile-run but i want to make sure that i am actually deleting the memory. it seems to actually add the class to it

thank you very much for your help

davide++
June 25th, 2008, 10:52 AM
Hi all.

There are some mistakes; for example you cannot use a string as index of array, and when you use the delete operator on arrays to deallocate the memory you must use the notation like this.
delete [] this->Registrar["blogs"];

Lindley
June 25th, 2008, 10:55 AM
for example you cannot use a string as index of array

No, but you can use it with that syntax if Registrar is a std::map.

MrPotatoes
June 25th, 2008, 12:53 PM
Hi all.

There are some mistakes; for example you cannot use a string as index of array, and when you use the delete operator on arrays to deallocate the memory you must use the notation like this.
delete [] this->Registrar["blogs"];
hmmm, this is an std::map so i do not think that notation is correct. i'm sure that i have to delete each one by hand no?

i should have mentioned that it was a map. that is my own fault

Lindley
June 25th, 2008, 01:29 PM
A map of what, exactly?

JohnW@Wessex
June 26th, 2008, 05:12 AM
Your 'new' and 'delete' may be unnecessary.
If your map was to contain BlogsController objects instead of pointers to BlogsController objects then you could have the following.

// this is a function within the class:
...
RegisterController("blogs");
...

// this is the actual function
void RegisterController(string Section)
{
Registrar[Section] = BlogsController();
}

When the map is destructed then the BlogsController objects are destructed too.

(This is all assuming that BlogsController is trivial to copy)

An alternative is Registrar.insert(std::pair<string, BlogsController>(Section, BlogsController());