C++ 中 func() 和 (*this).func() 的区别
Difference between func() and (*this).func() in C++
我正在用 C++ 编写其他人的代码,我发现对某个函数的奇怪调用 func()
。这是一个例子:
if(condition)
func();
else
(*this).func();
func()
和(*this).func()
有什么区别?
在什么情况下调用func()
和(*this).func()
会执行不同的代码?
在我的例子中,func()
不是宏。它是基础 class 中的虚函数,在基础和派生 class 中都有实现,并且没有自由 func()
。 if
位于基 class.
的方法中
实际上是有区别的,但在非常重要的上下文中。考虑这段代码:
void func ( )
{
std::cout << "Free function" << std::endl;
}
template <typename Derived>
struct test : Derived
{
void f ( )
{
func(); // 1
this->func(); // 2
}
};
struct derived
{
void func ( )
{
std::cout << "Method" << std::endl;
}
};
test<derived> t;
现在,如果我们调用t.f()
,test::f
的第一行将调用自由函数func
,而第二行将调用derived::func
.
无法从代码片段中分辨出来,但可能有 两个 可调用对象 称为 func()
。 (*this).func();
确保调用成员函数。
一个可调用对象可以是(例如)一个functor
或一个lambda
表达式:
函子
struct func_type
{
void operator()() const { /* do stuff */ }
};
func_type func; // called using func();
λ
auto func = [](){ /* do stuff */ }; // called using func();
例如:
#include <iostream>
class A
{
public:
// member
void func() { std::cout << "member function" << '\n'; }
void other()
{
// lambda
auto func = [](){ std::cout << "lambda function" << '\n'; };
func(); // calls lambda
(*this).func(); // calls member
}
};
int main()
{
A a;
a.other();
}
输出:
lambda function
member function
与类型相关的名称,可能会有所不同:
void func() { std::cout << "::func()\n"; }
struct S {
void func() const { std::cout << "S::func()\n"; }
};
template <typename T>
struct C : T
{
void foo() const {
func(); // Call ::func
(*this).func(); // Call S::func
}
};
这两行将调用不同函数的另一种情况:
#include <iostream>
namespace B
{ void foo() { std::cout << "namespace\n"; } }
struct A {
void foo() { std::cout << "member\n"; }
void bar()
{
using B::foo;
foo();
(*this).foo();
}
};
int main ()
{
A a;
a.bar();
}
我正在用 C++ 编写其他人的代码,我发现对某个函数的奇怪调用 func()
。这是一个例子:
if(condition)
func();
else
(*this).func();
func()
和(*this).func()
有什么区别?
在什么情况下调用func()
和(*this).func()
会执行不同的代码?
在我的例子中,func()
不是宏。它是基础 class 中的虚函数,在基础和派生 class 中都有实现,并且没有自由 func()
。 if
位于基 class.
实际上是有区别的,但在非常重要的上下文中。考虑这段代码:
void func ( )
{
std::cout << "Free function" << std::endl;
}
template <typename Derived>
struct test : Derived
{
void f ( )
{
func(); // 1
this->func(); // 2
}
};
struct derived
{
void func ( )
{
std::cout << "Method" << std::endl;
}
};
test<derived> t;
现在,如果我们调用t.f()
,test::f
的第一行将调用自由函数func
,而第二行将调用derived::func
.
无法从代码片段中分辨出来,但可能有 两个 可调用对象 称为 func()
。 (*this).func();
确保调用成员函数。
一个可调用对象可以是(例如)一个functor
或一个lambda
表达式:
函子
struct func_type
{
void operator()() const { /* do stuff */ }
};
func_type func; // called using func();
λ
auto func = [](){ /* do stuff */ }; // called using func();
例如:
#include <iostream>
class A
{
public:
// member
void func() { std::cout << "member function" << '\n'; }
void other()
{
// lambda
auto func = [](){ std::cout << "lambda function" << '\n'; };
func(); // calls lambda
(*this).func(); // calls member
}
};
int main()
{
A a;
a.other();
}
输出:
lambda function
member function
与类型相关的名称,可能会有所不同:
void func() { std::cout << "::func()\n"; }
struct S {
void func() const { std::cout << "S::func()\n"; }
};
template <typename T>
struct C : T
{
void foo() const {
func(); // Call ::func
(*this).func(); // Call S::func
}
};
这两行将调用不同函数的另一种情况:
#include <iostream>
namespace B
{ void foo() { std::cout << "namespace\n"; } }
struct A {
void foo() { std::cout << "member\n"; }
void bar()
{
using B::foo;
foo();
(*this).foo();
}
};
int main ()
{
A a;
a.bar();
}