In the below code there is a conversion operator for converting class A object into const class B object.
My question is when the const B object is been created, why its value change as
b.v=20 gives no error.
Probably, i am missing some thing.
Thanx in advance
class B
{
public:
int v;
B() : v(10) {}
};
class A
{
public:
operator B() const {}
};
void g(B b)
{
b.v=20;
}
int main()
{
A a;
g(a);
return 0;
}
constafter member function signature has nothing to do with return type; it only means that the function (or value returned by it) will not change state of the original object, i.e. will not change members other than those marked asmutable.Additionally, your function
g()takes parameter by value, so it’s copied anyway.