|
-
June 25th, 2008, 08:56 AM
#1
A Quick C++ Question
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)
Code:
// 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
-
June 25th, 2008, 10:52 AM
#2
Re: A Quick C++ Question
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"];
-
June 25th, 2008, 10:55 AM
#3
Re: A Quick C++ Question
 Originally Posted by davide++
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.
-
June 25th, 2008, 12:53 PM
#4
Re: A Quick C++ Question
 Originally Posted by davide++
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
-
June 25th, 2008, 01:29 PM
#5
-
June 26th, 2008, 05:12 AM
#6
Re: A Quick C++ Question
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.
Code:
// 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
Code:
Registrar.insert(std::pair<string, BlogsController>(Section, BlogsController());
Last edited by JohnW@Wessex; June 26th, 2008 at 08:18 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|