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();