简体   繁体   English

未定义的结构指针名称的 typdef 如何在 C++ 中工作?

[英]How does typdef of an undefined struct pointer name work in C++?

Let me explain the question with the help of code.让我在代码的帮助下解释这个问题。

I have following two files.我有以下两个文件。

cat ah猫啊

    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猫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 ah file : "typedef struct myIterStruct *myIter;"我无法理解的是 ah 文件中的第一行:“typedef struct myIterStruct *myIter;”

In the code, definition of struct myIterStruct is written nowhere, still this code compiles and works well.在代码中,struct myIterStruct 的定义没有写在任何地方,但这段代码仍然可以编译并运行良好。

Does the C++ compiler convert the undefined struct pointer to void*? C++ 编译器是否将未定义的结构指针转换为 void*? or is this specific to g++ compiler that I am using?或者这是否特定于我正在使用的 g++ 编译器? Please elaborate.请详细说明。

Thanks.谢谢。

It is enough to know that myIterStruct is a class or struct.知道myIterStruct是一个类或结构就足够了。 When the compiler sees struct myIterStruct it knows that and can form a pointer to it.当编译器看到struct myIterStruct它知道并可以形成指向它的指针。

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.其他一些指针,特别是void*char* ,可能会在一些不寻常的系统上使用额外的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM