create a set of objects in a function of a program

advertisements

could someone please tell me what I need to do in order to create an array of objects in a function (other than in the main function).

I will try to explain by making up some sort of example...

Let's say I have a program named TimeScheduler.cpp that implements the class Schedule.h (and I have the implementation in a separate file Schedule.cpp where we define the methods).

In the declaration file we have declared two constructors

  Schedule(); //the default

and

  Schedule(int, int, int);//accepts three arguments

to get to the point--let's say in the main program file TimeScheduler.cpp we created our own functions in this program apart from the functions inherited from the class Schedule. so we have our prototypes listed at the top.

 /*prototypes*/

  void makeSomeTime();

etc.....

we have

 main(){ 

//etc etc...
 }

we then define these program functions

    void makeSomeTime(){
      //process
    }

let's say that inside the function makeSomeTime(), we would like to create an array of Schedule objects like this

    Schedule ob[]={
       summer(5,14, 49),
       fall(9,25,50)
    };

what do I have to do to the function makeSomeTime() in order for it to allow me to create this array of objects. The reason I ask is currently i'm having difficulty with my own program in that it WILL allow me to create this array of objects in main()....but NOT in a function like I just gave an example of. The strange thing is it will allow me to create a dynamic array of objects in the function..... like

   Schedule *ob = new Schedule[n+1];
   ob[2]= Schedule(x,y,z);

Why would it let me assign to a non-dynamic array in main(), but not let me do that in the function?


This is not correct:

 Schedule ob[]={
       summer(5,14, 49),
       fall(9,25,50)
    };

You appear to be trying to introduce 3 new names:

  1. ob, which is an array of Scedules
  2. summer, which is a Schedule
  3. fall, which is a Schedule

You can't introduce summer and fall as new names like that. Perhaps this was just a typo, and you meant:

Schedule ob[]={
   Schedule(5,14, 49),
   Schedule(9,25,50)
};

...which is perfectly fine, and can exist in a function such as:

void make_schedule()
{
    Schedule ob[]={
       Schedule(5,14, 49),
       Schedule(9,25,50)
    };
}

But now you have another problem -- your make_schedule function returns void. The Schedule array you created in make_schedule is created and then just thrown away. If you want to return an array from a functtion, the best thing to do is to use a vector, and return that:

std::vector<Schedule> make_schedule()
{
    Schedule ob[]={
       Schedule(5,14, 49),
       Schedule(9,25,50)
    };

    const size_t num_obs = sizeof(ob)/sizeof(ob[0]);
    std::vector<Schedule> ret;
    std::copy( &ob[0], &ob[num_obs], std::back_inserter(ret));

    return ret;
}

A poorer alternative is to use dynamic allocation to allocate your array, and return a pointer to the first element. In this case, when using new [] it's important to note that you can only use the default constructor.