If malloc does not create a new object but only allocates raw memory, why am I able to access the class members via the pointer to this memory?
#include <iostream>
using namespace std;
const float PI = 3.141592654;
class Circle{
float radius;
public:
Circle(){
cout << "Constructor called";
}
~Circle(){
cout << "Destructor called";
}
void Radius(){
cout << "Enter radius: ";
cin >> radius;
}
float Area(){
return PI * radius * radius;
}
void Display(){
cout << "The circle with radius " << radius
<< " units has area = " << this->Area() << " unit" << "\xFD\n";
}
};
int main(){
Circle *mCircle = (Circle *)malloc(sizeof(Circle));
mCircle->Radius();
mCircle->Display();
return 0;
}
Can anyone cite a source to this: In C++ the rules state that an object isn't created until the constructor is called.