const修饰函数体的问题?
const修饰函数体,就是在函数后面加个const,是不是只用在类对象里面?
2021-09-07 08:47
程序代码:#include <iostream>
#include <source_location>
using namespace std;
struct foo
{
void bar() { clog << source_location::current().function_name() << '\n'; }
void bar() const { clog << source_location::current().function_name() << '\n'; }
void bar() volatile { clog << source_location::current().function_name() << '\n'; }
};
int main( void )
{
foo f;
const foo cf;
volatile foo vf;
f.bar(); // 输出 void foo::bar()
cf.bar(); // 输出 void foo::bar() const
vf.bar(); // 输出 void foo::bar() volatile
}
程序代码:#include <iostream>
#include <source_location>
using namespace std;
struct foo
{
void bar() & { clog << source_location::current().function_name() << '\n'; }
void bar() && { clog << source_location::current().function_name() << '\n'; }
};
int main( void )
{
foo f;
f.bar(); // 输出 void foo::bar() &
foo().bar(); // 输出 void foo::bar() &&
}
2021-09-07 10:46