I'm working on some code for a library. I want to code it as correctly as possible to reduse the likelihood that someone will accidently write bad code and mess up internal data structures. Anyone here is a simplification of the code I have.
Where am I losing const which would allow the code in main() to compile? Also, any suggestions on how to force B to have to be B const for it to compile?Code:class A
{
public:
int someValue;
};
class B
{
public:
B(A value)
: a(value)
{ }
void SetValue(int value)
{
a.someValue = value;
}
private:
A a;
};
class C
{
public:
C()
: a()
{}
B const GetA() const
{
B const b(a);
return b;
}
private:
A a;
};
int main(void)
{
C const c;
B b(c.GetA()); // this shouldn't work
b.SetValue(10); // nor this and C is now changed even though it's const and no typecasting was done
return 0;
}
Thanks,
Scott MacMaster
