creating an object within a function of a program

Posted by user1066524 on Stack Overflow See other posts from Stack Overflow or by user1066524
Published on 2012-06-18T21:13:41Z Indexed on 2012/06/18 21:16 UTC
Read the original article Hit count: 182

Filed under:
|
|

could someone please tell me what I need to do in order to create an object in a 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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about function