2

I have classes Date and Time. I am creating a class Onetime with a public constructor but i am not sure how i can create a new date and time inline.

schedule[0]= new Onetime("see the dentist", 
  Date(2013, 11, 4), 
  Time(11, 30, 0), 
  Time(12, 30, 0));

class Onetime {
  public: 
    Onetime(  // what should I put here?  )
}
user1082764
  • 1,973
  • 9
  • 26
  • 40
  • Avoid using new. Make schedule an array of Onetimes. Create a default destructor to allow an empty onetime. – Neil Kirk Sep 04 '13 at 16:45

3 Answers3

5

This the standard approach

class Onetime {
    std::string description;
    Date date;
    Time from_time;
    Time to_time;

    Onetime(const std::string& description,
            const Date& date,
            const Time& from_time,
            const Time& to_time)
    : description(description),
      date(date),
      from_time(from_time),
      to_time(to_time)
    {
       ... rest of initialization code, if needed ...
    }
  ...
};
6502
  • 112,025
  • 15
  • 165
  • 265
  • I don't think there's a need to get the arguments by reference, since the arguments are going to get copied to members of `Onetime` anyway. It's better to take them by value, since in that case the example given in the OP's code would result in no copies being performed. – Nikos C. Sep 04 '13 at 16:33
  • @NikosC.: I agree, but I wrote `const T&` parameters because this is the **standard** approach. It's indeed in my opinion an anti-pattern because a value is a value and a reference is a reference, but none the less is what is used even in the standard library. You can see my opinions about this issue in http://stackoverflow.com/a/4705871/320726. – 6502 Sep 04 '13 at 16:42
  • @NikosC. Please explain, I don't understand. – Neil Kirk Sep 04 '13 at 16:44
  • @NeilKirk See my comment on the answer 6502 linked to :-) – Nikos C. Sep 04 '13 at 16:55
  • @NikosC.IMHO, each object will be copied twice when passing by value not by reference. First copy is done while passing object as an argument and second copy is done while copying to class member. That's why passing by reference is the standard approach. Of course there are cases, when this approach is quite misleading, as was pointed out by 6502. – Wacek Sep 04 '13 at 17:44
  • @Wacek The links I provided in the comments of the linked to answer explain how passing by value results in no copy at all (due to move semantics or copy elision) and that passing by reference can result in a copy. The optimizer likes passing by value, not by reference. Not *always* of course, but in many, many cases. It's something well worth looking into. – Nikos C. Sep 04 '13 at 18:27
2

Declare the class as such, using either Date and Time or const Date & and const Time & for the date and time parameters. In general const & is appropriate for large objects to prevent them from being unnecessarily copied. Small objects can use const & or not, whichever you prefer.

// Onetime.h

class Onetime {
  public: 
    Onetime(const std::string &description,
            const Date &date,
            const Time &startTime,
            const Time &endTime);

  private:
    std::string description;
    Date        date;
    Time        startTime, endTime;
};

Then define the constructor in your .cpp file. Use : to denote an initializer list to initialize the member variables.

// Onetime.cpp

Onetime::Onetime(const std::string &description,
                 const Date &date,
                 const Time &startTime,
                 const Time &endTime)
    : description(description),
      date(date), startTime(startTime), endTime(endTime)
{
}

Finally, you can create a Onetime object exactly as you wrote. You could even omit the new keyword if you wish. new is for allocating objects on the heap, which you don't always need to do in C++ (unlike Java or C#, say).

schedule[0] = new Onetime("see the dentist", 
  Date(2013, 11, 4), 
  Time(11, 30, 0), 
  Time(12, 30, 0));
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

You can do,

class Onetime {
  public: 
    Onetime(const std::string& message, const Date& date, const Time& start, const Time& end);
  private:
    std::string m_message;
    Date m_date;
    Time m_start;
    Time m_end;
 }

and

Onetime::Onetime(const std::string& message, const Date& date, const Time& start, const Time& end)
: m_message(message), m_date(date), m_start(start), m_end(end) 
{

}

Try to avoid new as that is heap-allocated memory and expensive to get (compared to stack-based memory.)

apartridge
  • 1,790
  • 11
  • 18