Segmentation Error

Can anyone explain to me why I'm getting a segmentation error here? I haven't worked with pointers much, so I'm clueless about what I'm doing wrong.

This is the class I have
1
2
3
4
5
6
7
8
 class Date
{
    int *month;
    int *day;
    int *year;
public:
    Date();
};


This is the definition for the constructor Date()

1
2
3
4
5
6
7
8
9
10
11
Date::Date()
{
    int *month = new int;
    int *day = new int;
    int *year = new int;
    time_t now = time(0);
    tm* ptr = localtime(&now);
    *month = ptr->tm_mon + 1;
    *day = ptr->tm_mday;
    *year = ptr->tm_year + 1900;
}
The member variables in class Date will remain uninitialized. In the constructor you create local variables with the same name (which will shadow the member variables). So when you use the member variables later it will crash.
Topic archived. No new replies allowed.