Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

Assignment and const members

John_F_1
Beginner
170 Views

Consider the following code:

#include <iostream>

class MyClass
{
    const int value;

public:	
    explicit MyClass() :
    value(10)
    {
    }

    MyClass(int val) :
        value(val) 
    {
    }

    int GetValue()
    {
        return value;
    }
};

int main()
{
    MyClass a, b(20);

    std::cout << "a.value = " << a.GetValue() << " b.value = " << b.GetValue() << std::endl;
    b = a;
    std::cout << "a.value = " << a.GetValue() << " b.value = " << b.GetValue() << std::endl;

    return 0;
}

When built with Composer 2013 SP1 Update 4 on Windows, I get:

a.value = 10 b.value = 20
a.value = 10 b.value = 10

Two questions:

1. Why does this compile? Neither g++ nor MSVC will compile it because of the const member, which cannot be overwritten in the default assignment operator.

2. Why does assignment modify the const value?

 

0 Kudos
3 Replies
Kittur_G_Intel
Employee
170 Views

Hi John,

Yes, it shouldn't compile and should output an error on copying not allowed for assignment operator for const member value.  It worked fine on LInux with the latest icc version but on windows it didn't output the error.  I'll file this issue with the developers and will update you accordingly as soon as I've an update. Appreciate your patience till then.

_Regards, Kittur 

Marián__VooDooMan__M
New Contributor II
170 Views

Kittur Ganesh (Intel) wrote:

[...] I'll file this issue with the developers and will update you accordingly as soon as I've an update. Appreciate your patience till then.
 

Please, could we have a tracking number?

PS: I encourage all @Intel members to always write tracking numbers for bugs/feature requests, so we can check them in the release notes of the new version. TIA!

Kittur_G_Intel
Employee
170 Views

Sure, the tracking number is: DPD200361059  BTW, I'll update this post as soon as the release with the fix is out as well. Appreciate your patience through this, as always.

_Kittur 

Reply