I am not going to use this in practice. This is intended to understand the language spec.
I am trying to construct a C++ object in heap, using malloc and explicit initialization of fields. While for many interesting classes this is not possible without invoking UB, for some very simple classes, this seems legal to me. I thought to check if my understanding is correct (i.e. Does C++ somehow prevents the developer from bypassing constructor?). For example is the following legal?
class A {
public:
int a;
int b;
// constructor declaration and definition
// destructor declaration and definition
// other member functions.
};
int main() {
A *a = (A *) malloc (sizeof(A));
a->a = 1;
a->b = 2;
delete A;
}