GLBasic forum

Main forum => GLBasic - en => Topic started by: nabz32 on 2017-Jul-23

Title: GLBasic pointer question
Post by: nabz32 on 2017-Jul-23
Hello again,

only a small question, is it possible to build a linked list
in glbasic, something like:

Code (glbasic) Select

TYPE element

*element // store the next element in the list..

ENDTYPE


And then check if this Pointer is NULL?

Or is ist possible to access a c class inside the GLBasic code, without using EXTERN "C" all the time?
Having the abillity to write a linked list in GLBasic would be awesome.

Title: Re: GLBasic pointer question
Post by: Schranz0r on 2017-Jul-24
You can use INLINE and put a List into a DGArray, return the Index of it and u can use it in types without having problems.

Fun-Fact:
I've managed to put a wrapper of a c++ Class into a GLBasic Type... it was a try and it works perfectly :)
You can try to wrapp the Linked List into a GLB-TYpe.

I can try to send you an example later today.
Title: Re: GLBasic pointer question
Post by: nabz32 on 2017-Jul-24
thanks Schranz0r,

pretty good news for me  :good:

A small example would be greatly appreciated
Title: Re: GLBasic pointer question
Post by: nabz32 on 2017-Jul-26
I tried to create a class in inline, which compiles without errors,

I can create an object of it in INLINE,
and also create a DGArray of the class type.

But when I try to use DIMPUSH() on the DGArray in INLINE, I get countless errors suddenly.

Here is the Global INLINE Code:
Code (glbasic) Select

INLINE
class SEdgeC{
private:
DGInt x;
DGInt y;
SEdgeC* nextEdge;
public:
SEdgeC( DGInt xN , DGInt yN );
SEdgeC* getNextEdge();
void addNewEdge(DGInt x , DGInt y );
void deleteAll();
};
SEdgeC::SEdgeC( DGInt xN , DGInt yN ){
x = xN;
y = yN;
nextEdge = NULL;
}
SEdgeC* SEdgeC::getNextEdge(){
return nextEdge;
}
void SEdgeC::deleteAll(){
if( nextEdge != NULL ){
nextEdge->deleteAll();
}
delete this;
}
void SEdgeC::addNewEdge( DGInt xN , DGInt yN ){
if( nextEdge != NULL ){
nextEdge->addNewEdge( xN , yN ); // add nextEdge to my nextEdge, if I allready have one
} else nextEdge = new SEdgeC( xN , yN ); // if not then finally create the new edge
}
DGArray<SEdgeC> Sectors;
ENDINLINE


and the local INLINE code I used in my main function:
Code (glbasic) Select

INLINE
SEdgeC start( 0 , 0 );
start.addNewEdge( 0 , 0 );
ENDINLINE


this is all working, but when I call
Code (glbasic) Select
INLINE
DIMPUSH(Sectors,start);
ENDINLINE


I get tons of weird errors, telling me that every statement in the INLINE class is faulty.
Title: Re: GLBasic pointer question
Post by: Kitty Hello on 2017-Aug-04
Oh dear. You need a default constructor (no args).
You need a copy operator and a copy constructor. Take care what you do with the pointer so you don't have dangling pointers and multiple delete calls in the end.
The array is not going to be debugable (V15) this way - that's a bit more complicated.

What are you trying to achieve with this? You really think that a a linked list is faster than an array of types with line-array? It might be, but most times it's not, because the most time consuming usually is allocating memory. And an array has less calls to malloc/new than a linked list.
For very big data you are right, though, since you don't need the copies when you have to re-allocate more memory.