Author Topic: How to?  (Read 2056 times)

0 Members and 1 Guest are viewing this topic.

helloworld

  • Guest
How to?
« on: June 24, 2017, 09:52:00 PM »
How to declare a container of (pointers to) an abstract class/type (aka interface)?
That is the equivalent of C++:

Code: [Select]
struct Interface
{
virtual void f()=0;
};
struct A : Interface
{
void f(){}
};
struct B : Interface
{
void f(){}
};
int main()
{
Interface* ic[]={new A(), new B()};
}
« Last Edit: June 24, 2017, 10:15:38 PM by helloworld »

Charles Pegge

  • Guest
Re: How to?
« Reply #1 on: June 25, 2017, 08:10:07 PM »

It's quite complicated!

Code: [Select]
'2017-06-25 T 08:48:54
'ARRAY OF VIRTUAL OBJECTS
'
extern

'typical interface
'method act(){print "act"}
'...

class ca
method act(){print "act A"}
method constructor(){}
method destructor(){}
end class

class cb
method act(){print "act B"}
method constructor(){}
method destructor(){}
end class

extern virtual

class interface
  method act()
end class

end extern

new ca a1 : ?a1=@ca_table
new cb b1 : ?b1=@cb_table

typedef interface * pinterface
indexbase 1
pinterface vc[100]
@vc[]={@a1,@b1}

vc[1].act 'act A
vc[2].act 'act B
...
del a1
del b1

helloworld

  • Guest
Re: How to?
« Reply #2 on: June 27, 2017, 03:25:24 AM »
Great!

Arnold

  • Guest
Re: How to?
« Reply #3 on: June 27, 2017, 04:19:15 AM »
Hi Charles,

Quote
new ca a1 : ?a1=@ca_table
new cb b1 : ?b1=@cb_table

I assume _table is somehow built-in with Oxygenbasic and creates a pointer to a function table? Can this construct only be used with object oriented programming?

I am also not sure about the purpose of ?a1. Does this create an index? It seems to be different from @a1.

Roland

Charles Pegge

  • Guest
Re: How to?
« Reply #4 on: June 27, 2017, 05:02:39 AM »
Hi Roland,

class_table is only created when a class contains methods and other static members, but not for extern virtual objects.

External objects use method table pointers,  and currently, non-virtual  objects call methods use the table directly.

The '?' symbol is another way of casting (sys)

Charles Pegge

  • Guest
Re: How to?
« Reply #5 on: June 30, 2017, 02:17:36 PM »
This has similar functionality without requiring pointers.

Instead of building an array of virtual object pointers, a class is used to contain the objects.

members is used to invoke  methods in common of all the contained members with a single statement.

method act(){members.act}
method constructor(){members.constructor}
method destructor(){members.destructor}


Code: [Select]
'2017-06-30 T 07:29:27
'MEMBERS.METHOD()

class ca
int v
method act(){members.act : print "act A"}
method constructor(){}
method destructor(){}
end class

class cb
int v
method act(){members.act : print "act B"}
method constructor(){}
method destructor(){}
end class

'FLAT MEMBERS
class cc
has ca ca1,ca2
has cb cb1,cb2
method act(){members.act}
method constructor(){members.constructor}
method destructor(){members.destructor}
end class

new cc c
c.act
del c