CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Posts
    2

    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

  2. #2
    Join Date
    Jun 2006
    Posts
    437

    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"];

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: A Quick C++ Question

    Quote 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.

  4. #4
    Join Date
    Jun 2008
    Posts
    2

    Re: A Quick C++ Question

    Quote 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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: A Quick C++ Question

    A map of what, exactly?

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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
  •  





Click Here to Expand Forum to Full Width

Featured