Reputation: 101
I came across this in a program I am trying to improve.
class Class1
{
private:
public:
Class1();
~Class1();
protected:
Class2* Object2;
};
Imbalance::Imbalance()
{
Object2 = (Class2*)NULL;
}
Can someone tell me what the * means when you create the object and why when instantiating the class you would make the object = the class something null. Is there a book I can read on this? Or any good document or webpage on class and objects in MQL4, C++ ...
Upvotes: 1
Views: 1597
Reputation: 73500
The *
in C++ mean it's a pointer to an object.
Initializing a pointer to NULL
means that it's pointing to nowhere ( by the way nullptr
would be a better alternative nowadays ).
Usually, at a moment in your code, you'll find some statement like:
if ( Object2 == NULL ) // if not pointing to an object
Object2 = new Class2; // create a new one
Most books on C++ explain pointers in depth.
So my first answer would be "The C++ programming language" from B.Stroustrup.
Some online tutorials: here and here.
Edit: MQL4 language
In MQL4 language, which was historically based on a C-like syntax constructs and recent (post Build 509 ) extensions brought some more ( borrowed from MQL5 domain ), the *
is also a pointer to an object, and new
creates an object dynamically. But unlike C++, it's not a direct pointer to a memory place, but an indirect pointer using the concept of a descriptor.
A predefined constant variable NULL
means -- like in C++ -- that there is no value. It can be assigned to variables of any other fundamental types without conversion. The comparison of fundamental type variables with the NULL
value is allowed.
In "New-MQL4" ( post Build 509 ) NULL
can also be compared to pointers to objects created with the new
operator.
(Thanks to user3666197 for the additional MQL4 specific info)
Upvotes: 3