Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Methods in types
#2
You're talking about classes? A class is a udt that can have member functions.

Classes work by having a hidden extra parameter for member functions, a "this" pointer. That way, the member function knows which object it is working on.

A similar effect can be had in procedural languages by adding the extra parameter yourself. This is similar to what the win32 api does using handles, implementing object orientation using a procedural interface.

Here is a simple, but not practical, example in qbasic 1.1:

Code: (Select All)
DECLARE SUB DrawCritter (this AS ANY)
DECLARE SUB EraseCritter (this AS ANY)
DECLARE SUB IterateCritter (this AS ANY)
DECLARE SUB CloseCritter (this AS ANY)
DECLARE SUB CreateCritter (this AS ANY)

TYPE critter
l AS INTEGER
c AS INTEGER
age AS INTEGER
letter AS INTEGER
END TYPE

CLS
DIM crit AS critter
CreateCritter crit
WHILE crit.age < &H10
EraseCritter crit
IterateCritter crit
DrawCritter crit
SLEEP 1
WEND
CloseCritter crit
SYSTEM

SUB CloseCritter (this AS critter)
this.l = 0
this.c = 0
this.age = 0
this.letter = 0
END SUB

SUB CreateCritter (this AS critter)
this.l = INT(RND * 25) + 1
this.c = INT(RND * 80) + 1
this.age = 1
this.letter = INT(RND * 26) + &H61
END SUB

SUB DrawCritter (this AS critter)
LOCATE this.l, this.c
COLOR this.age AND &HF, 0
PRINT CHR$(this.letter);
END SUB

SUB EraseCritter (this AS critter)
LOCATE this.l, this.c
COLOR 7, 0
PRINT " ";
END SUB

SUB IterateCritter (this AS critter)
this.l = this.l + INT(RND * 3) - 1
this.c = this.c + INT(RND * 3) - 1
IF this.l < 1 THEN this.l = 1
IF this.l > 25 THEN this.l = 25
IF this.c < 1 THEN this.c = 1
IF this.c > 80 THEN this.c = 80
this.age = this.age + 1
END SUB

One of the problems with doing this in qbasic 1.1 is a lack of proper pointer support. Yeah, qbasic has varptr, peek, and poke, but it doesn't have proper typed pointer support in the language.

To be practical, you would have to use an array, and manage the allocation within the array manually. In qb64, you could use _mem blocks.

Another consideration, if you're accustomed to c++, is that c++ will manage object lifetime automatically for you, calling it "resource allocation is initialization". C++ will automatically call the constructors and destructors for you. In particular, the destructor is called whenever the scope is exited, whether by a return, by an end of block, or by an exception. In qbasic, you would have to do all of that manually.

The win32 api uses handle counts. So, calling CloseHandle on an object decrements a handle counter. Only when the last handle is closed is the object actually destroyed.

All of the above applies to implementing oop within a procedural language. As for modifying qb64 to accommodate classes, i don't know. Obviously, it would need to involve a hidden "this" pointer. And it would probably need to involve implementing raii by calling constructors and destructors automatically. Since qb64 translates to c++, it might be possible to use c++'s own raii for this, but i don't know.
Reply


Messages In This Thread
Methods in types - by bobalooie - 01-17-2025, 01:02 AM
RE: Methods in types - by mcalkins - 01-22-2025, 05:38 AM
RE: Methods in types - by bobalooie - Yesterday, 07:45 PM
RE: Methods in types - by hsiangch_ong - 01-22-2025, 05:50 AM
RE: Methods in types - by James D Jarvis - 01-25-2025, 04:36 PM
RE: Methods in types - by hsiangch_ong - 01-26-2025, 12:26 AM
RE: Methods in types - by bplus - 01-26-2025, 12:08 AM
RE: Methods in types - by Pete - Yesterday, 08:00 PM



Users browsing this thread: 3 Guest(s)