Thread: How to make an array of class with constructor using pointer

  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
    28,413
    One solution:
    Code:
    std::vector<ScriptMarker> StartMarkers(n, pMission);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. using a class constructor w/ class array
    By matt000r000 in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2009, 07: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