How to make an array of class with constructor using pointer

This is a discussion on How to make an array of class with constructor using pointer within the C++ Programming forums, part of the General Programming Boards category; I have a class named ScriptMarker which is initialised by Mission as a parameter in the constructor. ScriptMarker* pStartMarker = ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    How to make an array of class with constructor using pointer

    I have a class named ScriptMarker which is initialised by Mission as a parameter in the constructor.

    ScriptMarker* pStartMarker = new ScriptMarker(pMission);

    And it is deleted this way.

    delete pStartMarker;
    pStartMarker = NULL;

    So how will I able to make an array of class all initialised with the same parameter and how will I be able to delete it safely?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,438
    One solution:
    Code:
    std::vector<ScriptMarker> StartMarkers(n, pMission);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    You can't do what you want directly with the array form of operator new and delete. There are some hacks you can use but, generally speaking, they often do not qualify as "safe" (a term that has various aspects).

    There is, however, no need to directly use operator new or delete directly. That is inherently the dangerous approach, whether you are managing one dynamically allocated object or a million. Use a standard container instead.
    Code:
    #include <vector>
    
    int main()
    {
       std::vector<ScriptMarker> container(number_you_need, ScriptMarker(pMission));
    
         // do whatever you need with elements of the container
         //   even resize the container if you need to
    
        //  as we return, the container and its elements are cleaned up "safely".
       return 0;
    }
    The container will be safely destructed (and its elements) when it passes out of scope.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Thank you laserlight and grumpy
    But
    I need to delete it at the middle of function because the destructor does some important task too before the cleanup. Is there anyway?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Resize the container to zero.

    Better yet, break your function into smaller parts (implemented in separate functions) so the container is cleaned up when you need it to be.

    A primary advantage of the standard containers is that their lifetime can be controlled using scope - if a function that creates a container returns, the container is cleaned up. If you have a function with two parts (one part that needs a container, and another part that doesn't) those two parts can be placed into separate functions.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 01:44 AM
  2. using a class constructor w/ class array
    By matt000r000 in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2009, 06:08 AM
  3. Passing array through class constructor
    By sonicdoomx in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 01:42 PM
  4. class array constructor help
    By cfrost in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2004, 02:08 PM
  5. (structure+array+pointer)to make a simple database
    By frankie in forum C Programming
    Replies: 5
    Last Post: 04-26-2002, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21