我想知道在 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/