0

我对这段代码有疑问,这是来自迷宫程序的头文件(stack.h)。我正在研究堆栈结构,在我的文档中,我无法理解这些类型的结构,谁能向我解释为什么我们使用 typedef 以及第 12 行和第 21 行是如何工作的?

    #ifndef STACK_H
    #define STACK_H 
    #define STACKSIZE 50 

    typedef struct d {
        int x;
        int y;
        int right; int left;
        int down;
        int up;
        int camefrom;
        } StackDataType, position;           /// LINE 12

    struct STACK{
        StackDataType element[STACKSIZE]; int top;
        void create();
        void close();
        bool push(StackDataType); StackDataType pop();
        bool isempty();
    };
    typedef struct STACK Stack;             /// LINE 21
    #endif
4

5 回答 5

1

I think you do not need to typedef a struct again in C++, it again defines a struct, which is unnecessary. You can just define:

struct d{

};
于 2013-11-10T20:37:23.420 回答
1

In my (considerable) experience, this almost always denotes a C programmer who has fumbled their way into C++. If these are notes from your classes, it doesn't bode well.

In the earliest "C", if you declared a struct

struct StructName {
    int a;
    int b;
};

This didn't declare a type name, it only declared a struct name, so to make an instance of StructName you would have to write:

struct StructName myStruct;

If you wanted to be able to omit the "StructName" part you would need to use a typedef:

struct StructName { int a, b; };
typedef struct StructName StructName;

Or you could combine these into one, somewhat confusing, statement:

typedef struct StructName { int a, b; } StructName;

I say confusing because if the struct definition is many lines long, it could be confused for a second C syntax which lets you declare an instance of a Struct after defining the type:

struct StructName { int a, b; } StructName;
// aka
struct StructName { int a, b; };
struct StructName StructName; // local variable, StructName of type struct StructName
// declare a VARIABLE called StructName which is of type anonymous-struct.
struct { int a, b; } StructName;

One problem with this is that you can't use the typedef'd name in the structure declaration:

// Won't compile because 'List' isn't declared until the end.
typedef struct list_structure { List* next; int a; } List;

// Won't compile because you have to remember to say 'struct List'
typedef struct List { List* next; int a; } List;

// Compiles
typedef struct list_structure { struct list_structure* next; int a; } List;

This confused a lot of C programmers. Enough so that many C programmers will tell you that the definition of a struct is

typedef struct tag_name { /* struct details */ } structname;
//e.g.
typedef struct tagStructName { int a, b; } StructName;

C++ inherited all of this, but also went ahead and made the typedef implied for you:

// doesn't compile as C, will compile as C++
struct List {
    List* next;
    int a;
};

To see it not compiling as C: http://ideone.com/3r9TRy

In C++, declaring something as a class is exactly the same as declaring it a struct, with one change:

class List {
    List* next;
public:
    int a;
};

Is EXACTLY as though you had written:

struct List {
private:
    List* next;
public:
    int a;
};

There's no other difference between a struct and a class in C++.

于 2013-11-10T20:57:02.103 回答
0
于 2013-11-10T20:34:31.620 回答
0

In C++ you don't need typedef like that. In fact it's weird now. Below code is enough to make a new type:

struct foo {
     ...
};

 

In old days, you had to explicitly typedef structures to avoid using struct everywhere. For example:

typedef struct {
} foo;

// or

struct foo {
};
typedef struct foo foo;

otherwise you had to use struct before any usage of foo:

struct foo f;

In face struct foo together was a type and foo was not (unless you explicitly made it a type by typedef)

 


Finally, your code can be like this:

#ifndef STACK_H
#define STACK_H

#define STACKSIZE 50

struct StackDataType
{
    int x;
    int y;
    int right;
    int left;
    int down;
    int up;
    int camefrom;
};

// typedef StackDataType position;

struct Stack
{
    StackDataType element[STACKSIZE];
    int top;
    void create();
    void close();
    bool push(StackDataType);
    StackDataType pop();
    bool isempty();
};

#endif
于 2013-11-10T20:34:40.720 回答
0

What's going on is that essentially the typedef is being used to create a shorthand way to refer to the given structure.

So in this example, both StackDataType and position are shorthand references to what is formally declared as struct d, and Stack is a shorthand reference to what is formally declared as struct STACK.

Generally speaking, this allows for cleaner code referencing these structures. E.g., instead of having to write:

struct STACK var;

to declare an instance of this structure, you can just use:

Stack var;

You can declare a typedef either at the same point at which you declare the type (as in the first example), or you can declare it later (as in the second).

于 2013-11-10T20:37:32.227 回答