据我所知,const修饰符应该从右向左阅读。由此,我得到了这样的结论:
const char*
是一个指针,它的char元素不能修改,但指针本身可以修改,并且
char const*
是指向mutable字符的常量指针。
但我得到了以下代码的以下错误:
const char* x = new char[20];
x = new char[30]; //this works, as expected
x[0] = 'a'; //gives an error as expected
char const* y = new char[20];
y = new c
我有一个函数,它以函数指针作为参数,然后用自己的参数调用该函数:
typedef int (*my_func_ptr)( int );
int foo( my_func_ptr f ) {
static int i = 0;
return i = f( i );
}
有时,我需要将函数传递给foo,这些函数不仅依赖于整数输入,还需要输出结果。
int add_strlen( int i, const char* s ) {
return i + strlen( s );
}
我可以重新处理上面的代码来使用std::function,然后使用std::bind,但是最好
以下代码在编译时引发编译器错误。
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
// maximum of two C-strings (call-by-value)
inline char const* max (char const* a, char const* b)
{
return strcmp(a,b) < 0 ? b : a;
}
// maximum of three va