1

I have a class Date. let date be:

class Date
{
private:
    unsigned int _day;
    unsigned int _month;
    unsigned int _year;
public:
    const unsigned int& Day;
    const unsigned int& Month;
    const unsigned int& Year;

    Date() : Day(_day), Month(_month), Year(_year)
    {  }
}

For some reason, after the constructor is called Day, Month and Year do not point/refer to _day, _month and _year.

One guess of mine would be that they are being set before memory is allocated to the class, how would i go about solving this issue (aka setting the references after memory allocation)?

Thanks in advance!

EDIT: More info

The value of _day (for eg) is not returned when i get the value of Day. I get a seemingly random number.

Sellorio
  • 1,806
  • 1
  • 16
  • 32

3 Answers3

4

It's not very clear what you want to achieve. In your Date class you can access _date, _month, _year directly why do you want to set another reference?

But to answer you question

The value of _day (for eg) is not returned when i get the value of Day. I get a seemingly random number

Actually, the values are being returned, but you are getting garbage because _day, _month, and _year are just uninitialized integers. You need to initialize them in the initializer list first:

Date() : _day(0), _month(1), _year(2), Day(_day), Month(_month), Year(_year)
ipmcc
  • 29,581
  • 5
  • 84
  • 147
billz
  • 44,644
  • 9
  • 83
  • 100
  • _date, _month, _year are inaccessable since i dont want people to set invalid values to them. The const ref is to provide a readable, simple interface to getting the information in the fields (get and not edit). Hmm let me try that solution.. i likey :) – Sellorio Feb 06 '13 at 01:07
1

You should expose them using getters returning const references to avoid having to store them, it is much more convenient.

class Date {
private:
    unsigned int _day;
    unsigned int _month;
    unsigned int _year;

public:
    const unsigned int& Day(){return _day;}
    const unsigned int& Month(){return _month;}
    const unsigned int& Year(){return _year;}
}
Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
  • I am looking for this cause its prettier. If it cant be done i will do as you say but i prefer to do it like this. – Sellorio Feb 06 '13 at 00:56
  • The getters are traditional C++ syntax. The references have a per-instance memory cost and a performance cost as well. They are a poor solution just to avoid the characters `()`. – StilesCrisis Feb 06 '13 at 02:10
  • Readability in many cases is more important than the unnoticeable performance difference which is MAYBE measurable in the microseconds. – Sellorio Feb 06 '13 at 02:21
0
Date::Date(unsigned int myDay, unsigned int myMonth, unsigned int myYear)
{
    //Assign values here in constructor.
}

You can write separate methods to return the day, month, or year in the class, or any combination of them for any different date formats that you have planned in the design doc.

Theo20185
  • 56
  • 2
  • Baptiste Wicht has already said that. Im looking for a way to make this work. Getters are less readable. Also any insights to what is the actual cause (i only had a guess) would be nice :) – Sellorio Feb 06 '13 at 01:05
  • 1
    Yeah, he beat me by 7 seconds. :D – Theo20185 Feb 06 '13 at 01:19