Research: How to do classes in Lite-C

Posted By: MasterQ32

Research: How to do classes in Lite-C - 03/18/13 23:38

Hey!

I was just playing around with Lite-C when i remembered that Lite-C supports C++ API with Classes (like DirectX9).
The syntax was pretty easy and i made some tests.
My results: You can have classes in Lite-C nativly, but with a different setup as in C++.

So how do i create a class?
Just check this code example:
Code:
#include <acknex.h>

typedef struct ITestVtbl
{	
	int answer;
	void sayHello(void *This);
} ITestVtbl;

typedef interface ITest
{
	ITestVtbl *lpVtbl;
} ITest;

void ITest_sayHello(ITest *This)
{
	printf("Answer to live: %d", This->answer);
}

ITest *ITestNew()
{
	ITest *test = sys_malloc(sizeof(ITest));
	test->lpVtbl = sys_malloc(sizeof(ITestVtbl));
	test->lpVtbl->sayHello = ITest_sayHello;
	return test;
}

void main()
{
	ITest *test = ITestNew();
	test->answer = 42;
	test->sayHello();
}



The class name is ITest and you create it with ITestNew().
You can have fields and methods, so have fun with it.

Greetings
Felix
Posted By: txesmi

Re: Research: How to do classes in Lite-C - 03/19/13 08:42

Very intereting. I did a search for further info about 'interface' keyword but did not find an explanation. Could you point us?

Thanks!
Posted By: sivan

Re: Research: How to do classes in Lite-C - 03/19/13 08:56

thanks. a while ago I thought of adding voids to a struct to elimitate some if-else branching. I would never find out that interface thingie...
Posted By: Arrovs

Re: Research: How to do classes in Lite-C - 03/20/13 05:30

I wish it could be possible like this:
http://www.pvv.org/~hakonhal/main.cgi/c/classes/

Possible it is, but im not found it in Lite-c.

I will sure test out interface, but i think it will not do everyhing what i want.
Posted By: MasterQ32

Re: Research: How to do classes in Lite-C - 03/20/13 11:35

@Arrovs:
This is possible, but you need Lite-C syntax and pointers instead of normal c syntax and copying structs.

Interface could be found at "DLLs and API" in the manual but not very well documented.

@JustSid:
The function signature is copied from the manual, i didn't change that. Also this whole thing isn't compatible to MSVC at all i think. It appears that the Lite-C Compiler just takes the interface, adds -[>lpVtbl] to it and [&interface] as the first parameter of the function call.
And the member is in the vtbl because you cannot access if it is declared in the interface itself.
© 2024 lite-C Forums