Let me explain the question with the help of code.
I have following two files.
cat a.h
typedef struct myIterStruct* myIter;
class myIterator;
class myList
{
int mVal;
myList* mNext;
friend class myIterator;
public:
myList(myList* next, int val) : mVal(val), mNext(next) {}
~myList() {}
void AddTail(int val) {
myList* newnode = new myList(NULL, val);
myList* tail = this->GetTail();
tail->mNext = newnode;
}
myList* GetTail() {
myList* node = this;
while (node->mNext)
node = node->mNext;
return node;
}
myList* GetNext() { return mNext; }
int GetVal() { return mVal; }
};
class myIterator
{
myList* mList;
public:
myIterator(myList* list) : mList(list) {}
~myIterator() {}
int next() {
int ret = -1;
if (mList) {
ret = mList->GetVal();
mList = mList->GetNext();
}
return ret;
}
};
cat main.cxx
#include <iostream>
#include "a.h"
using namespace std;
myIter createIterator(myList* list)
{
myIterator *returnitr = new myIterator(list);
return (myIter) returnitr;
}
int myListGetNextNode(myIter iter)
{
if (iter == NULL)
return -1;
myIterator* funciter = (myIterator *) iter;
return funciter->next();
}
int main()
{
myList* list = new myList(NULL, 1);
list->AddTail(2);
list->AddTail(3);
myIter iter = createIterator(list);
int val = -1;
while((val = myListGetNextNode(iter)) != -1) {
cout << val << '\t';
}
cout << endl;
return 0;
}
This code is used in a project to implement list and iterator. What I am not able to understand is first line in a.h file : "typedef struct myIterStruct *myIter;"
In the code, definition of struct myIterStruct is written nowhere, still this code compiles and works well.
Does the C++ compiler convert the undefined struct pointer to void*? or is this specific to g++ compiler that I am using? Please elaborate.
Thanks.