This code throws an error:
class A {};
class B : public A {};
int main() {
A instance = new B();
}
Error message:
void *operator new(unsigned int)
no suitable constructor exists to convert from "B *" to "A"
My first language was Java and I'm really confused about this. Every subclass can be treated as its motherclass because it derives everything, so why shouldn't they add the option to treat B like A?
Or is there a mistake in my syntax? I started learning C++ 3 weaks ago, it could be possible.
I wasn't able to find a solution with google or by searching through my tutorial, so please help me.