GLBasic forum

Main forum => GLBasic - en => Topic started by: MrTAToad on 2010-Sep-26

Title: REQUIRE
Post by: MrTAToad on 2010-Sep-26
One thing that I never mentioned for some reason when I was doing the Guichan system, is that I found that when the GLBasic was compiling, it  was REQUIREing a lot of C++ class, some of which had constructors, which appeared to be called during the compiling phase, rather than when the program is run (and the class created).

Will a C++ class have its constructor initialised whilst the program is being compiled ?
Title: Re: REQUIRE
Post by: MrTAToad on 2010-Sep-26
Yes, the former is what I was describing :)

I found it when I was putting some code in a constructor, which called, I think another class.  As the second one hadn't be initialised before I was calling it (as it would have been when the program was run), the program instantly crashed...
Title: Re: REQUIRE
Post by: hardyx on 2010-Sep-27
You call another class in the constructor of a class. There is no problem if you create the other class with his constructor. Constructors are for initializing variables, don't make a big process there. If the other class must to be initialized by other methods, you must to check it with a boolean variable or a pointer to avoid errors.

Code (glbasic) Select
MyClass::MyClass()
{
   AnotherClass a(); // calls AnotherClass constructor
   a.SomeMethod();
}


or using a global class instance:

Code (glbasic) Select
static AnotherClass *a = NULL;

MyClass::MyClass()
{
   if (a == NULL)
       a = new AnotherClass(); // construct a
   a->SomeMethod();
}