#include <iostream>
#include <cstdlib>
using namespace std;
class Box {
public:
Box() {
cout << "Constructor called!" <<endl;
}
void printer(int x)
{
cout<<x<<" printer"<<endl;
}
~Box() {
cout << "Destructor called!" <<endl;
}
};
int main( ) {
Box* myBoxArray = new Box[4];
Box* myBoxArray2 = (Box*)malloc(sizeof(Box[4]));
myBoxArray2->printer(23);
*myBoxArray2;
*(myBoxArray2).printer(23);
return 0;
}
the problem simply is that when i use 'new' the constructor is printed out but when i simple derefrence the pointer to myBoxArray2
the constructor is not printed and neither is the funtion printer
printed.
Also why is it that when i use ->
the funnction printer runs but not when i use the equivalent *(myBoxArray2).printer(23)