4
I wanted to understand basically what the logic behind the objects of the classes that use the operator new
for example, I have the following program in D:
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 work in the language for the programmer to do h.print()
and not h->print()
?
The compiler generates more code behind and makes allocation or the language developer defines this way of working in the language itself?
You could simulate it in C++ like this?
Hello h = new Hello();
h.print();
instead of:
Hello *h = new Hello();
h->print();
Very interesting I have to take shame in the face and get the nose learns pointer, allocation, relocation and memory management..
– dark777
But how so "will make it harder later when you need to pass the pointer to the object in other functions" if I understand correctly you mean that it would make it difficult to use this pointer to create a new object is this?
– dark777
@dark777 I refer to situations where functions are waiting for pointers and you have objects come back to fetch the address to call the function. I’m basically talking at the syntax level. But in all the codes we see in C++ is always used the
new
and I keep the value in a pointer, is what is considered good practice– Isac
I understood I created this topic about doubt to understand if such a feat was possible and if it was how he would work back if it did only memory allocation or reallocation or if it gave return of *this within some kind of constructor for such but already elucidated me well the doubt... Thanks for the feedbacks ....
– dark777
@dark777 No problem. It is always important to understand and deepen the pointer and memory part as it is vital in C/C++. Good studies.
– Isac