user2386317
user2386317

Reputation: 33

C++ defining const in constructor

I am getting error on line 30 (const Date date2 = new Date(31, 12, 2012);)

Error message is : conversion from ‘Date*’ to non-scalar type ‘const Date’ requested

Below is my source code:

class Date
{
private :
    int day ;
    int month ;
    int year ;
public :
    Date(){
        day = 1;
        month = 1;
        year = 2000;
    }
    Date(int d, int m, int y) : day(d), month(m), year(y){
    }
    int getDay () const { return day ;}
    int getMonth () const { return month ;}
    int getYear () const { return year ;}
};

int main ()
{
    const Date date ;
    const Date date2 = new Date(31, 12, 2012);

    cout <<"The month is "<< date.getMonth()<< endl ;
    cout << " The month is "<<date2.getMonth()<< endl ;
    return 0;
}

Upvotes: 0

Views: 177

Answers (5)

David G
David G

Reputation: 96800

 const Date date2 = new Date(31, 12, 2012);

This line results in an error because date2 has the type Date but the result of the right-hand side has the type Date*. This is because of the way new works.

Per Paragraph 5.3.4/2 of the C++11 Standard:

Entities created by a new-expression have dynamic storage duration (3.7.4). [ -- ] If the entity is a non-array object, the new-expression returns a pointer to the object created. If it is an array, the new-expression returns a pointer to the initial element of the array.

So now we know that new allocates memory for its operand on the heap and returns a stack-allocated pointer to it. You might of had a misconception that new creates an object (like in Java, JavaScript or C#) but this is not the case in C++. For the above line to work you would have to make date2 a pointer:

const Date *date2 = new Date(31, 12, 2012);

However, the normal way of creating an object is to simply do the following (in your case):

const Date date2(31, 12, 2012);

Upvotes: 0

Astro - Amit
Astro - Amit

Reputation: 767

Problem is in your code:

1.const Date date2 = new Date(31, 12, 2012);

Here you are dynamically allocating memory for object Date.Operator new will return pointer so that you need to take in to the pointer type of object example

const Date *date2 = new Date(31, 12, 2012);

2.cout << " The month is " << date2.getMonth()<< endl ;

If you modify the code as per point 1 then above line has to be changed as So if date2 is pointer then you need to call function as :

cout <<"The month is"<<date2->getMonth()<<endl ;

3.If you want to use your code then you just remove new from below line:

const Date date2 = Date(31, 12, 2012);

You need to modify your code as :

int main ()
{
    const Date date ;
    const Date *date2 = new Date(31, 12, 2012);
    cout << " The month is " << date.getMonth() << endl ;
    cout << " The month is " << date2->getMonth() << endl ;
    return 0;
}

or

int main ()
{
    const Date date ;
    const Date date2 = Date(31, 12, 2012);
    cout << " The month is " << date.getMonth() << endl ;
    cout << " The month is " << date2.getMonth() << endl ;
    return 0;
}

Upvotes: 1

Jiaming Lu
Jiaming Lu

Reputation: 885

operator new returns a pointer, if you really need a pointer, you should use

const Date* date2 = new Date(31,12,2012);

instead. And don't forget to delete date2.

Or you can do either:

const Date date2(31,12,2012);

or

const Date date2 = Date(31,12,2012);

Upvotes: 0

JBL
JBL

Reputation: 12907

Well, using new returns a pointer that you try to assign to a non-pointer const variable.

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281405

You need to do this:

const Date date2(31, 12, 2012);

In your code, const Date date2 is a Date whereas new Date(31, 12, 2012); returns a pointer to a Date (which leaks, incidentally).

Upvotes: 6

Related Questions