首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何引用在C++中称为函数的内容?

如何引用在C++中称为函数的内容?
EN

Stack Overflow用户
提问于 2012-11-16 08:44:26
回答 3查看 85关注 0票数 1

我想知道在C++中是否有一种方法可以知道一个函数的名称是什么?比如Java或JavaScript中的this关键字。

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

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

代码语言:javascript
复制
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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-16 08:47:35

对于historical reasonsthis是一个指针。使用->而不是.

代码语言:javascript
复制
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))

票数 2
EN

Stack Overflow用户

发布于 2012-11-16 08:48:45

this在c++中的工作方式与在Java中的工作方式相同。唯一的区别是你需要使用this->而不是this.this是一个指针,因此你不能使用点运算符来访问它的成员。

票数 1
EN

Stack Overflow用户

发布于 2012-11-16 08:47:21

为什么不直接调用linked_list::insert(int)中的其他函数呢?不,它是无效的,您应该用this -> something代替this.something

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13408767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档