I am trying to implement an ArrayList (a dynamic array used in java) using C in Object-Oriented style. I have defined the following struct. (Consider this as a pseudo code)
struct ArrayList{
/*structure member declarations...
...
*/
void (*add)(); /*function pointer which
points to the function which will add a new element at the end of the list*/
};
/*Function to create an ArrayList "object"*/
struct ArrayList* newArrayList(){
/*prepare an ArrayList "object" and return*/
return arrayList;
}
My question is, is it possible to do something like
struct ArrayList* aL=newArrayList();
aL->add(element); /*I want to do it like this*/
aL->add(&aL, element); /*And not like this*/
I don't want to pass the reference of ArrayList again.
I thought of using a static struct ArrayList*
variable inside the implementation of add function so that I can initialize it once and it will be used in the subsequent function calls, but then I thought it will create a mess when I create
struct ArrayList* aL2=newArrayList();
aL2->add(element);
I know we can write Object-Oriented code in C to some extent.
But is it possible to do aL->add(element);
like, the way we access a method in Object-Oriented language?