return const 而不是函数重载?
Function overloading by return const and not?
我正在阅读 HELib 的源代码并遇到以下 class definition:
#define CLONED_PTR_DECLARE(CLONED_PTR_TYPE,CLONED_PTR_INIT) \
\
template <class X, class Cloner = CLONED_PTR_INIT<X> > class CLONED_PTR_TYPE \
{ \
......
const X* get_ptr() const { return ptr; } \
X* get_ptr() { return ptr; } \
......
}; \
\
但据我了解,C++ 没有 return 值的函数重载。那么这两个get_ptr()
函数的作用是什么呢?
不是return类型,是const关键字。如果您在 class 的 const 实例上调用该方法,将调用 const 方法。
我正在阅读 HELib 的源代码并遇到以下 class definition:
#define CLONED_PTR_DECLARE(CLONED_PTR_TYPE,CLONED_PTR_INIT) \
\
template <class X, class Cloner = CLONED_PTR_INIT<X> > class CLONED_PTR_TYPE \
{ \
......
const X* get_ptr() const { return ptr; } \
X* get_ptr() { return ptr; } \
......
}; \
\
但据我了解,C++ 没有 return 值的函数重载。那么这两个get_ptr()
函数的作用是什么呢?
不是return类型,是const关键字。如果您在 class 的 const 实例上调用该方法,将调用 const 方法。