Hello StackOverflowers,
With a friend, we had following problem recently. There was a base class:
class A {
public:
A() : foo(10) {}
virtual int getFoo() const { return foo; }
protected:
int foo;
};
A friend implemented a class deriving from the one above.
class B : public A {
public:
void process() { foo = 666; }
protected:
//int foo;
};
Unfortunatelly he also added field foo in descendent class (commented line). So the following code.
#include <iostream>
int main()
{
A* aaa= NULL;
if (1) {
B* bbb = new B;
bbb->process();
aaa = bbb;
}
std::cout << aaa->getFoo() << std::endl;
return 0;
}
printed 10.
That's not the problem, since this will be totally redesigned, and such things won't happen in future.
I was just wondering, do you know any (portable) tricks or language patterns (besides obvious getters/setters; btw they were there actually, with foo being private), that would disallow declaring variable with the same name in descendent class (e.g. by causing compile-time error).
TIA!