简体   繁体   English

指针语法C ++

[英]Pointer Syntax C++

Regarding binary trees - I had seen the following code as part of the solution for a problem: 关于二叉树 - 我已经看到以下代码作为问题解决方案的一部分:

struct Node
{
    int key;
    struct Node *left, *right;
};

My question is what does "struct Node *left, *right" mean given it is defined w/in the body of the first struct Node definition. 我的问题是“struct Node * left,* right”是什么意思,因为它是在第一个struct Node定义的主体中定义的。 Also if this is C++ why would you use struct instead of just class/object here? 如果这是C ++,为什么你会在这里使用struct而不仅仅是class / object?

In C++ (and especially C), types are usually always represented by their construct. 在C ++(尤其是C)中,类型通常总是由它们的构造表示。

For instance, 例如,

enum Foo { ... };
void doSomethingWithAFoo(enum Foo f);

struct Bar { ... };
void doSomethingWithABar(struct Bar bar);

While this is required in C, it is not in C++. 虽然这在C中是必需的,但它不是在C ++中。 In C, it is achieved by using a typedef . 在C中,它是通过使用typedef实现的。

typedef struct { ... } Foo; // Can now be referenced with just `Foo`

However, there is a particular part of the spec that states that struct types cannot have instance of themselves inside of them (more specifically it states types cannot refer to themselves before they're fully declared). 但是,规范的某个特定部分指出结构类型不能在它们内部拥有自己的实例(更具体地说,它们表示类型在它们被完全声明之前不能引用它们自己)。

Except in pointer form. 指针形式除外。 This is because pointers are known sizes at the beginning of compilation, whereas structures are only known after they are declared. 这是因为指针在编译开始时是已知的大小,而结构只有在声明它们之后才知道。


Since struct s pre-date C++ (only by a little) and have been present since ANSI C (C89) and before in most major compilers, they are also present in C++ (since ANSI C can be compiled gracefully in compliant C++ compilers). 由于struct s早于C ++(仅有一点点)并且自ANSI C(C89)以及之前在大多数主要编译器中都存在,它们也存在于C ++中(因为ANSI C可以在兼容的C ++编译器中优雅地编译)。

However, C++ adds the concept of classes, which don't exist in C. As others have mentioned, classes and structs are similar in that they both holds members. 但是,C ++添加了C语言中存在的类的概念。正如其他人所提到的,类和结构类似,因为它们都包含成员。 In C++, structs can have methods just like classes - obviously this is not the case in C. 在C ++中,结构体可以像类一样使用方法 - 显然在C中不是这种情况。

The only difference as far as I'm aware is the visibility; 据我所知,唯一的区别是能见度; struct s default to public and class es default to private. struct s默认为public,而class es默认为private。 C does not have the notion of visibility. C没有可见性的概念。

Means exactly what it looks like. 意味着它的样子。 A recursive declaration just means a Node struct had two fields that are pointers to other Nodes. 递归声明只是意味着Node结构有两个字段,这些字段是指向其他节点的指针。

I've read that the main difference between structs and classes in C++ is the default permissions (structs are all public by default). 我已经读过C ++中结构和类之间的主要区别是默认权限(默认情况下结构都是公共的)。 Slightly simpler, especially since inheriting is unlikely. 稍微简单一点,特别是因为继承不太可能。

The meaning of struct Node *left, *right; struct Node *left, *right;的含义struct Node *left, *right; within the definition of struct Node is that each instance of struct Node contains members ( left and right ) that point to other struct Node s. 的定义内struct Node是,每个实例struct Node包含的成员( leftright ),该指向其他struct Node秒。

It is the programmer's responsibility to ensure that, whenever a struct Node is created, that those members are initialised appropriately. 程序员有责任确保在创建struct Node ,适当地初始化这些成员。 They may be set to NULL (indicating they don't point at any object) or to the address of another struct Node . 它们可以设置为NULL (表示它们不指向任何对象)或另一个struct Node的地址。

In C++, initialisation of left and right will often be done in a constructor. 在C ++中,初始化leftright经常会在构造函数中完成的。 C doesn't have that feature so, every time some C code creates a struct Node , it will typically have to explicitly initialise the left and right members. C没有这个特性,因此,每当某些C代码创建struct Node ,它通常必须显式初始化左右成员。

The code you have shown is really C, although it will be accepted by a C++ compiler. 您显示的代码实际上是C,但它将被C ++编译器接受。 C++ allows the declaration struct Node *left, *right; C ++允许声明struct Node *left, *right; to omit the struct keyword. 省略struct关键字。 C does not. C没有。

In C++, a struct and a class are the same thing - the difference is accessibility of members: C++ struct members are public and class members are private by default. 在C ++中, structclass是相同的 - 不同之处在于成员的可访问性:默认情况下,C ++ struct成员是publicclass成员是private的。 C++ struct s (and class es) are able to do numerous other things that C struct s cannot. C ++ struct (和class es)能够做许多C struct不能做的事情。

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

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