Operator operation new

3

I wanted to understand basically what logic behind the objects of the classes that use the new operator for example, I have the following program in D language:

import std.stdio;

class Hello
{
 public this(){} //construtor

 public void print()
 {
  writeln("Hello World");
 }
}

void main()
{
  Hello h = new Hello();
  h.print();
}

In C ++

#include <iostream>

class Hello
{
  public:
  Hello(){} //construtor

 void print()
 {
   std::cout<<"Hello World";
 }
}

void main()
{
  Hello *h = new Hello();
  h->print();
}

How does this object pointer allocation in the language work so that the programmer does h.print() and not h->print() ?

Does the compiler generate more code behind and allocate or does the language developer define this way of working in the language itself?

Would you like to simulate this in C ++ doing so?

Hello h = new Hello();
h.print();

instead of:

Hello *h = new Hello();
h->print();
    
asked by anonymous 15.09.2017 / 06:52

2 answers

4

I begin by stating that:

ponteiro->metodo();

Corresponds to

(*ponteiro).metodo();

And that's what's called syntactic sugar , that is, a more convenient way for the programmer to do the same.

By analyzing the statement, we see that you are getting the value pointed to by the pointer with * and on that value you access the metodo .

It means that the example you submitted could also be written like this:

Hello *h = new Hello();
(*h).print();

However it would be much less practical.

Operator new

The new operator in C ++ always returns the memory address where the object was placed by the memory allocator. This is equivalent to malloc in C.

And so you always see code that stores the result of new in a pointer, just like the one it presented:

Hello *h = new Hello();
h->print();

But it is not mandatory. You could save only the value pointed by the received pointer, like this:

Hello h = *(new Hello()); //atente no * para obter o objeto que o ponteiro aponta
h.print();

But it ends up not really simplifying the use and will make it difficult later when you need to pass the pointer to the object in other functions

    
15.09.2017 / 11:43
3

There is a huge difference between C ++ and D. D always creates classes by reference and allocates in the managed heap . In C ++ a class is just a structure ( struct ) whose members are private by default, but nothing. If you want to allocate in the unmanaged heap you must use new to say this to the compiler. In D it's just to make it readable that it is your very intention.

On the other hand in modern C ++ it is rare to use this form, nowadays one uses semi-managed memory with smart pointers .

In general compilers generate "more" codes all the time, it's one of their main functions.

Allocation is done by the new operator and there. Internally the operator probably uses the malloc() function, but not necessarily. You can define your own new operator in the class.

C ++ likes to be explicit about how to access the object whereas D does not care about this, so trying to use the dot operator instead of the arrow is gambiarra and not idiomatic in C ++.

    
15.09.2017 / 13:12