titanandrews
Programmer
Hi,
I am a little confused about what the difference is when you use new to instantiate an object versus not using new. In the Java world you can only create objects using new, but it seems that C++ is different. Using the sample code below, I get the same output both ways. My book does not do a good job explaining the difference. Can someone please enlighten me?
Thanks!
Barry
ANewObj.cpp
I am a little confused about what the difference is when you use new to instantiate an object versus not using new. In the Java world you can only create objects using new, but it seems that C++ is different. Using the sample code below, I get the same output both ways. My book does not do a good job explaining the difference. Can someone please enlighten me?
Thanks!
Barry
ANewObj.cpp
Code:
#include <iostream>
using namespace std;
class ANewObj
{
public:
ANewObj::ANewObj()
{
cout << "ctor called\n\n";
}
ANewObj::~ANewObj()
{
cout << "dtor called\n\n";
}
void someMethod()
{
cout << "someMethod called\n\n";
}
};
int main(int argc,char *argv[])
{
cout << "Using new " << endl;
ANewObj *myObj = new ANewObj();
myObj->someMethod();
delete myObj;
cout << "Not using new " << endl;
ANewObj anotherObj;
anotherObj.someMethod();
return(0);
}