Apparently, the new
operator returns void*
. So I was wondering what mechanism allows void*
casting to A*
when calling new A()
to create a new instance of class A
.
To illustrate:
// Example program
#include <iostream>
#include <string>
class A
{
public:
A() {}
};
void* createA()
{
return new A();
}
int main()
{
A* a1 = new A();
A* a2 = createA();
delete a1;
delete a2;
}
A* a1 = new A()
compiles fine (obviously).
A* a2 = createA()
does not reporting error: error: invalid conversion from 'void*' to 'A*'
(obviously too...).
Actually, the first one also does a conversion from void*
to A
. What mechanism or rule allows the first conversion and rejects the second one?
That's probably a stupid question....if so, sorry about that.