b=a->b 在一类列表中是什么意思?

What does b=a->b means in a class of list?

本文关键字:一类 列表 意思 是什么 gt      更新时间:2023-10-16

在一类列表中b=a->b是什么意思?

我正在阅读的例子是列表的析构函数,在它的"while"循环中有这个操作。

Clistint::~Clistint(){ 
  Clist *actual, *next; 
  if(head!=NULL){ 
    actual=head; 
    while(actual!=NULL){ 
      next=actual->next; 
      delete actual; 
      actual=next; 
    } 
  } 
}

EDIT(现在您已经发布了代码)

next=actual->next; 
delete actual; 
actual=next; 

这将把actual设置为actual.next。你不能只做

actual=actual->next;

因为这将是内存泄漏(您永远不会删除旧的actual)。而且,你不能只写

next=actual.next;

因为actual是一个指针。因此你必须得到它所指向的东西,比如

next=(*actual).next;

但是->运算符就是这样做的,所以你可以直接做

next=actual->next; // means the same thing as "next=(*actual).next;"

(早前发布的文章)

意思相同
b = (*a).b;

将局部变量b设置为指针ab的值。例如:

MyClass *a = new MyClass;
// do stuff with *a
int b;
b = a -> b; // gets the `b` value of `a` (assuming MyClass has a public int b)
            // same as "b = (*a).b;"

a是指向结构体或类的指针,a->b是该结构体或类中的某个东西,可能是一个变量。还有一个名为b的局部作用域变量,它被赋值为结构体ab的值。如果没有看到完整的代码,很难说更多。

首先,=->是c++语言的基本内置运算符。这些操作符的含义可以在任何有关C或c++的书籍中找到。

其次,在c++中,=->都是可重载的操作符,这意味着如果不知道ab是什么,就无法确定b = a->b的确切含义。