我想知道在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;
}发布于 2012-11-16 08:47:35
对于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))。
发布于 2012-11-16 08:48:45
this在c++中的工作方式与在Java中的工作方式相同。唯一的区别是你需要使用this->而不是this.,this是一个指针,因此你不能使用点运算符来访问它的成员。
发布于 2012-11-16 08:47:21
为什么不直接调用linked_list::insert(int)中的其他函数呢?不,它是无效的,您应该用this -> something代替this.something
https://stackoverflow.com/questions/13408767
复制相似问题