Creating an Object

Hello,
My classes structure is as follows:

Test.h
1
2
3
4
5
6
7
class Test
{
public:
    Test();
    ~Test();
    int value;
};


Test.cpp
1
2
3
4
5
6
7
8
9
10
Test::Test()
{
   value=10;
   cout<<"Test constructor called";
}

Test::~Test()
{
   cout<<"Test Destructor called";
}


Main.cpp
1
2
3
4
5
6
int main()
{
   Test* t=new Test();   // constructor is called
   Test* tt=(Test*)malloc(sizeof(Test)); // constructor is not called
   return 0;
}



In line 1 of main() , with help of new memory is also allocated for Test pointer and it's Object is also created.

However, in line 2 how can i associate creating an object of Test to pointer tt?
Here, only memory is only allocated for pointer tt, but object is not created.

help appreciated
amal
You can't. malloc/free are from C, which is not OO and thus do not support calling ctor and dtor. Use new/delete when writing C++.
Topic archived. No new replies allowed.