tags:

views:

274

answers:

6
+5  A: 

It's because you may not know at compile time whether you need an object, or how many, or of what type. The new operator allows you to dynamically allocate objects without having to know such things in advance.

Here's an example of not knowing the type of object in advance:

class Account { ... };
class CheckingAccount : public Account { ... };
class VisaAccount : public Account { ... };

...

Account *acct = type == "checking" ? new CheckingAccount : new VisaAccount;
Marcelo Cantos
It doesn't allow you to generate objects of types that are determined at runtime. The type must be known in advance. As for "how many", one can also use standard containers.
sellibitze
@sellibitze, what type of object does the `acct` variable point to at runtime? And how do you think standard containers allocate memory for their elements?
Marcelo Cantos
+6  A: 

It's a matter of object lifetime: if you stack-allocate your objects, the objects destructors will be called when these objects go out of scope (say, at the end of the method). This means that if you pass these objects out of the method that created them, you'll find yourself with pointers to memory that could be overwritten at any time.

Guido Domenici
Why the downvote?
Drew Hall
+3  A: 

The main reason you'll need to use new/delete is to get manual control the lifetime of your objects.

Other reasons are already provided by others but I think it's the more important. Using the stack, you know exatly the lifetime of your objects. But if you want an object to be destroyed only after an event occurs, then it cannot be automatically defined.

Klaim
Why the downvote?
Drew Hall
A: 

[updated]

You can use new to create new instance of some class, or allocate memory (for array for example), like

Object o = new Object();

Before creating new instance of class Object, you cannot use it. (Unless you have static methods.)(this is just one example of usage, sometimes other objects will instantiate or destroy objects that you need/don't need)

There are many good answers here but it is difficult to explain everything about new in one reply on SO, and if you do not understand what happens when new is called then it is difficult to know when to use it. This is one of the most important areas in programming so, after reading basic information here you should study it in more detail. Here is one of possible articles where you could start your research:

http://en.wikipedia.org/wiki/New_%28C%2B%2B%29

Topics that you will have to learn in order to understand what happens when you call new, so that you then could understand when to call it (there are more probably but this is what i can think of now): - constructors (and destructors) - static classes and methods ...

If it's too long to explain why do you bother to write here?
Artem Barger
Hi Keith, thanks for comment, but please comment my answer and not your interpretation of it. If you read my answer carefully you will notice that I never said that you *must* use NEW, and only NEW, to create an instance of a class. Only that NEW is used to create new instance of a class, which is true.It is also true that one cannot use a class before you instantiate it, unless there are some static things (which i also mention), but then I realized that there are so many things to say about this important subject that the best I can do is point to a better source (answer for Artem).
@zarko-o Actually what you said was "You need new to create new instance of some class", which is incorrect.
meagar
@meagar I would always like to learn new things. Can you please explain what is wrong in that, that might help me increase understanding.
@zarko-o The issue is that you can create object instances without `new`, you don't *need* it which is how you opened your answer.
meagar
@meagar I see what you mean - wording updated.
@zarko-o I think that why your answer was reacted to so negatively, is that Java programmers migrating to C++ often use new for every object, which is not how things should be done in C++. So I think it was an overreaction in the name of removing incorrect information from SO.
KeithB
+2  A: 

The lifetime of the data/objects created on the stack is restricted to the block. You cannot return references/pointers to it. For data to be available across functions, you can create it on the heap with new. Of course, it can be at a higher level, or even global. But you seldom know at compile time how much data/how many objects will be needed at run time.

AngryWhenHungry
A: 

You can write a many non-trivial programs without ever calling "new." (or thus delete).

What you wouldn't be able to do (at least without writing or using your own equivalents) is decide what type of objects or how many you want to create at run-time, so you'd be limiting yourslef.

JohnMcG