-1

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.

Dharmendra
  • 384
  • 1
  • 5
  • 22
  • 1
    you forward declare the struct. it should change when you change the typedef to this `typedef myIterStruct *myIter;` – Hayt Nov 16 '16 at 15:24

1 Answers1

1

It is enough to know that myIterStruct is a class or struct. When the compiler sees struct myIterStruct it knows that and can form a pointer to it.

The rule that this must work, indirectly forces the compiler use the same size for all pointers to class/struct.

Some other pointers, particularly void*and char*, might use additional bytes on some unusual systems.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 1
    I can't imagine a platform where `sizeof(char*)` != `sizeof(RandomType*)`. Do you have an example? – SergeyA Nov 16 '16 at 15:52
  • Thanks a lot Bo !! – Dharmendra Nov 16 '16 at 15:54
  • @SergeyA - We used to have word addressed machines, where each word would contain several chars. A `char*` might then need an index in addition to the word address. – Bo Persson Nov 16 '16 at 16:07
  • @BoPersson, but pointer to every object is representable as a char*. Where would those indexes come from in this case? – SergeyA Nov 16 '16 at 16:19
  • @SergeyA - That would be up to the implementer. It is just *allowed* to have different sizes for different types of pointers, see this [quote from the C standard](http://stackoverflow.com/a/1241314/597607). – Bo Persson Nov 16 '16 at 17:32
  • @BoPersson, I am not disputing the fact that it is allowed. But I am asking a practical question, based on your experience. – SergeyA Nov 16 '16 at 18:35