Can someone explain to me the following compiler error, which says that 'x' is an ambiguous reference?
Why can the compiler not allow this, if it knows that one of those variables is actually inaccessible?
class A {
int x; // private here
};
class B {
public:
int x; // public here
};
class C : public A, public B {
};
int main() {
C c;
c.x = 5; // here is the error
return 0;
}
Edit: For people who explain to me that private does not mean that it cannot be changed - I know that, and made this simple example below, but this is not the case I was asking about.
//
// the goal is to hack x, y values
//
#include <stdio.h>
#include <memory>
class A {
int x;
int _r;
double y;
public:
A() { x = 1; y = 0.5; _r = 0; }
void foo() { printf("x = %d, y = %lf, _r = %d\n", x, y, _r); }
};
int main() {
struct _B {
int x = 2;
double y = 1.5;
} b;
A a;
a.foo(); // gives "x = 1, y = 0.500000, _r = 0"
memcpy(&a, &b, sizeof(_B)); // where the sizeof B is eq 16 (4 for int, 4 for padding, 8 for double)
memcpy(&a, &b, sizeof(b.x) + sizeof(b.y)); // that is undefined behaviour, in this case _r is overridden
a.foo(); // gives "x = 2, y = 1.500000, _r = -858993460" (_r is just a part of floating point y value but with invalid cast)
return 0;
}