> Struct embedding is simple composition, not inheritance.
In C++, composition differs from inheritance because when you embed an object inside another object, you cannot call its method directly, but through a wrapper:
class Dog {
Animal a;
bool IsAlive() {
return this->a.IsAlive()
}
}
However, in Go, you can call their methods directly:
type Dog struct {
Animal
}
func main() {
Dog.IsAlive()
}
So the distinction itself isn't really meaningful. In Go, composition
is inheritance.
How else do you define inheritance, if not as ability to call methods of the inherited type?
> If you embed an "Animal" in a "Dog" struct, the "Animal" struct doesn't have any means to find out it's a "Dog", i.e. it can never find out at runtime that it's embedded in a "Dog" object or rely on functionality defined in the "Dog" object
I'm not quite clear on what you mean. How exactly does this work in C++? How do you "detect" it's a Dog on runtime, or "rely" on functionality defined in Dog?
If you can provide code example, that'd make things much easier for me to understand.