May 02, 2024, 09:35:11 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Class Constructor Errors

Started by Zen, January 11, 2006, 05:59:57 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zen

Hi Paul.

I have made a few classes and for some reason if i declare my class variable outside of the main subroutine my class constructor is not being called. So whenever i am calling a method i need to call the class constructor manually first. This is happening in my Raw internet class. The constructor initializes the winsock library so that you dont have to do it manualy. For some reason it doesnt work outside of main or another subroutine.

Any ideas?

Lewis

Ionic Wind Support Team

Global classes are not supported yet.
Ionic Wind Support Team

Zen

Sorry Paul can you explain in a little more detail please as i dont quite understand.

Lewis

Ionic Wind Support Team

Class constructors/destructors are called only if the class in instanced as a local subroutine variable or with NEW.  If you define a class instance in global program scope then the compiler has nowhere to put the code to call the constrructor, or set up the vtable.

Myclass cl;  //global scope doesn't work

global sub main()
{
MyClass cl2; //local scope works fine.
}
Ionic Wind Support Team

Zen

Any idea when this will be fixed?

Lewis

Ionic Wind Support Team

Nope.  Probably not until the beta.

The compiler will have to inject code into your main subroutine to construct any global classes.  The problem is if your using a project, and you compile a different file with global classes there is no where for the compiler to inject code.  It will require a lot of thinking on my part ot get it to work right.

Create you classes dynamically, or just use a global pointer which is more common with us C tyoes;

global g_cl
MyClass *g_cl;  //OK since it's a pointer

global sub main()
{
MyClass cl;
g_cl = &cl;  //valid for life of the application now;l


}

Then in some other file you can use the extern keyword.

extern g_cl as pointer;

You'll need to typecase it here since extern doesn't undersand the "*" syntax yet.

Paul.
Ionic Wind Support Team

Parker

Is it possible to do
extern int x;
or only by using the 'AS' keyword?

Ionic Wind Support Team

Not yet.  Haven't changed the extern syntax since the beginning.
Ionic Wind Support Team