-5

What does this syntax mean?

date aaa(2011,7,1);

Instead of aaa I can use whatever I want (for example bbb or ccc). It looks like I create a new object called aaa and belonging to the class date. But us far as I known I need to use the new keyword to create a new object.

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 4
    please read at least one c++ tutorial for beginners. It is hard to learn a language just by asking questions. – Zdeslav Vojkovic Mar 20 '13 at 10:50
  • This is very long to explain. I think you should grab a book about C++ and read the chapter about object creation. – alestanis Mar 20 '13 at 10:50
  • 1
    What the others said. Learn yourself some C++ before asking C++ questions. This is *way too basic* ("How do I move the turtle in Logo?" anyone?). Have a look at [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – R. Martinho Fernandes Mar 20 '13 at 10:52
  • Repeat one hundred times: "C++ is not Java." Then get a book about C++. Not Java. – Pete Becker Mar 20 '13 at 11:03
  • @Pete Becker, I do not use Java. I saw `new` in the C++ examples. – Roman Mar 20 '13 at 11:07
  • 3
    Fair enough. It's a common misunderstanding from Java programmers when they grow up. – Pete Becker Mar 20 '13 at 11:09

4 Answers4

7

It means "create an instance of data called aaa by calling a constructor of date which takes three integer parameters (or parameters that can be implicitly constructed from int)".

In C++, you do not need the new keyword to create an object. new instantiates an object in dynamic storage and returns you a pointer to it. Amongst other things, it means that you (or someone, somewhere) has to take responsibility for deleting the object at some point.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
5
int x;

I'm sure you understand that this defines an int object named x. Did you use new? How about if we add copy-initialization:

int x = 5;

This defines an int object named x and initialises it to 5. The same result can be achieved with direct-initialization:

int x(5);

So what about:

data aaa(2011,7,1);

This is precisely like the above syntax. We're creating an object of type data called aaa, just this time we're passing three arguments to its constructor.

The thing that all of these definitions have in common is that they create an object of a certain type with automatic storage duration. This means that the objects will be destroyed at the end of their scope.

Now consider the following line:

int* p = new int(5);

This creates two objects. The first is an int* called p. That int* has automatic storage duration, just as above, and will be destroyed at the end of its scope. We then initialise that object with the pointer returned by new int(5). This new-expression creates an int object, initialized to 5, with dynamic storage duration. That means that it is not destroyed automatically at the end of its scope, but needs to be deleted.

So, in short, these are two different ways to create objects that give them different storage durations.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • `int x(5);` does initialize object to 5. `int x = 5;` does not. It creates and object and assigns 5 to it. There is a semantic difference between, calling a constructor `T()` and `operator=(int)` versus `T(int)`. OP asked question on such an elementary level I think it's best to keep answers super strict. Or am I wrong and `T(int)` would be called ? – luk32 Mar 20 '13 at 10:57
  • @luk32 `int x = 5;` does not do any assignment - it is *copy-initialization* and has precisely the same effect (in this case) as doing `int x(5);`. No `operator=` is used here. – Joseph Mansfield Mar 20 '13 at 10:58
  • Ok my bad then. Sorry. I though those are semantically different. – luk32 Mar 20 '13 at 10:58
  • Uhm. OK, they ARE semantically different. I know that in case of `int` the result is the same. But it can be very different and copy-/direct- initializations are not interchangeable in general case. That was my original point, although I was wrong what actually happens. Thanks for pointing it out. – luk32 Mar 20 '13 at 11:12
  • 1
    @luk32 if the types on the LHS and RHS are different, then they are semantically different. There still is no assignment involved though. – juanchopanza Mar 20 '13 at 11:26
1

In some languages similar to C++ you might need new to create objects, for example Java. However in C++ you don't have to.

In C++ you use new to create a new object on the heap, for example if you need to create one dynamically during runtime. If you declare a variable such as the one in your example, it will be created on the stack if it's a local variable inside a function. Those objects are only available as long as their scope lasts.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The new operator instantiate an object in heap memory and that object exist until delete is called. In your example the object is instantiated in the stack and exist only in the scope (until the next }).

Alessandro Pezzato
  • 8,603
  • 5
  • 45
  • 63