Reputation: 59
what would be the definition of copy assignment operator of a class if a class having const member variable and a reference member variable eg:
class ABC
{
int const i;
int & j;
int k;
public :
ABC() :k(40), i(10),j(k)
{}
};
Upvotes: 1
Views: 213
Reputation: 238311
Const objects and references are non-assignable. Therefore a class that has such members will not have implicit assignment operators. It is possible to define a custom assignment operator, but not in a way that modifies those members.
If you want your class to have a copy assignment that makes the reference to refer to the same object as was referred by the argument object, then you simply cannot use a reference for that purpose.
If you want your class to have a copy assignment that makes a const member to have the same value as the argument object, then you simply cannot use a const member.
for const we can do it with the help of const_cast
Modifying a const object (with help of const_cast
) has undefined behaviour.
Upvotes: 1