c++ - 如何引用 C++ 中所谓的函数?

标签 c++ algorithm linked-list

我想知道在 C++ 中是否有一种方法可以知道什么叫做函数?就像 Java 或 JavaScript 中的 this 关键字。

例如,我有一个名为 insert 的函数,它将一个项目插入到链表中,我希望调用这些函数 insert 的链表调用另外两个函数。我该怎么做?

我现在有这个,这个有效吗?

bool linked_list::insert( int i )
{
    bool inserted = false;

    if( this.is_present(i) ) /* function is_present is defined earlier checks if an int is already in the linked-list. */
    {
        inserted = true // already inside the linked-list
    }
    else
    {
        this.Put( 0, i ); /* function Put is defined earlier and puts an int in a linked-list at a given position (first param.). */
        inserted = true; // it was put it.

    }
return inserted;
}

最佳答案

对于 historical reasons , this 是一个指针。使用 -> 而不是

bool linked_list::insert(int i) {
    bool inserted = false;

    if(this->is_present(i)) {
        inserted = true; // fixed syntax error while I was at it.
    } else {
        this->put(0, i); // fixed inconsistent naming while I was at it.
        inserted = true;
    }
    return inserted;
}

通常根本不需要使用this->;你可以只做if(is_present(i))

关于c++ - 如何引用 C++ 中所谓的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408767/

相关文章:

c++ - 什么是快速插入+删除和部分反转的好数据结构?

java - 如何使用 get 方法从链表中获取元素

c++ - template <typename T> 遍历整个代码

c++ - 以优雅的方式进行二进制比较操作

c++ - 什么时候应该动态或自动实例化对象?

c++ - 无法在使用 malloc/placement new 创建的类上调用虚函数

c# - 列表的唯一组合

javascript - 根据长度过滤和排序字符串数组?

algorithm - 伙伴内存系统中的最坏情况外部碎片

c - C 中的指针和在链表中的使用