Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Assignment and const members

John_F_1
Beginner
441 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
KitturGanesh
Employee
441 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 

0 Kudos
Marián__VooDooMan__M
New Contributor II
441 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!

0 Kudos
KitturGanesh
Employee
441 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 

0 Kudos
Reply