To solve your problem as written, you could use a virtual parent so that it's shared as one instance in the child class:
class Parent {
public:
int var;
};
class A : public virtual Parent {}; // ** Note I added virtual here
class B : public virtual Parent {}; // ** Note I added virtual here
class C : public A, public B {
void setVar(int v) {
var = v; // No longer ambiguous.
}
};
But this smells of a design that could use another look. Why do two unrelated classes both have the same data within them (in your original design)? Think about these things a bit and perhaps an alternate design will come to you (abstract interface in a base class, mechanism to tie the classes together using template algorithms, etc). Alternately if you provide more information about the relationship of your classes we can provide a C++-idiomatic solution.
EDIT:
Another idea that I believe I like better would be to seperate the position's state from the physics and collidable objects, adding one more level of indirection. So physics and collidable both would have pointers to an external position state object. Since it's external, both would refer to and be able to mutate the same shared state. The desired ownership would dictate the type of pointer used.
A rough sketch might look like this:
class Position {};
class A
{
public:
explicit A(const shared_ptr<Position>& pos) : pos_(pos) {}
private:
shared_ptr<Position> pos_;
};
class B
{
public:
explicit B(const shared_ptr<Position>& pos) : pos_(pos) {}
private:
shared_ptr<Position> pos_;
};
class C : public A, public B
{
public:
explicit C(const shared_ptr<Position>& pos) : A(pos), B(pos) {}
};
int main()
{
shared_ptr<Position> pos = make_shared<Position>();
C object_of_type_C(pos);
}
Finally note that is-a
, while a useful guideline, shouldn't really determine when you inherit. What it really means is that you want to take a C
and transparently use it as a physics OR as a collidable without the code in question really knowing it's a C
. If this represents your need then that's the real test for your design (see Liskov Substitution Principle).