-1

I have a class name MyDate with the following members and methods:

class MyDate
{
    public:
        MyDate();
        void setDay(int d);
        void setMonth(int m);
        void setYear(int y);
        void set(int day_, int month_, int year_);
        void print ();

    private:
    int day;
    int month;
    int year;

};

I have another class names Calendar which has an array of pointers to MyDate.

class Calendar 
{
    public:
        Calendar() ;
        void setDate (int num);
        bool isFree(int num_);
        int firstFree();
        void insertDate(MyDate my_date_);

        void print ();

    private:
        std::array<MyDate*, 30> m_dates;

};

I implement the insert function in the following way:

void Calendar :: insertDate(MyDate my_date)
{
int f = firstFree()-1 ;//the first free index in the array
*m_dates[f]=my_date; //is there a way to implement it without getters from //MyDate class??
}

I know that I can't do *m_dates[f]=my_date;--->just to explain what I have to implement. is there a way to implement it without getters from MyDate class??

sarit rotshild
  • 733
  • 2
  • 10
  • 19

2 Answers2

1

You can't use *m_dates[f]=my_date; because the array only has pointers, so you still need to provide storage space for the actual objects. If you can and want to use smart an array of smart pointers (std::array<std::shared_ptr<MyDate>, 30> m_dates;) , you can use m_dates[f].reset(new MyDate(my_date));, otherwise you have to take care of memory management.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • @stefaanv- but the question is how should I implement the assignment without getters? – sarit rotshild Mar 08 '16 at 11:10
  • @saritrotshild: there are no getters in my answer. You can do `MyDate date = oldDate;`, but when using pointers, you have to provide storage space. – stefaanv Mar 08 '16 at 11:13
  • @ stefaanv- what to you mean by provide storage spaces? please demonstrate. – sarit rotshild Mar 08 '16 at 11:15
  • A pointer can either point to some existing objects on which the pointer normally has no ownership, or the pointer points to an object that is new-ed as in m answer, where storage space is allocated and the values of my_data are copied to. – stefaanv Mar 08 '16 at 12:14
  • @saritrotshild: smart pointers: `m_dates[f].reset(new MyDate(my_date));`, ordinary pointers: `m_dates[f] = new MyDate(my_date);` with extra code to make sure you don't leak memory if m_data[f] already points to a new-ed object. The smart pointer case was already in my answer. – stefaanv Mar 08 '16 at 13:28
0

First of all your array is keeping the pointers to the mydate objects and you have to properly manage the data. If I look at the interface that you use to insertDate, insertDate(myDate *my_date) would have been clear and you can use simply m_dates[f]=my_date to set the values to the array. You might have to make sure the pointers which are set valid during the program and myData objects are properly managed if your intention is to keep pointers. As mentioned in one comment in C++11 you can take the advantage of shared_pointers so you do not have to manage memory explicitly.