h****b 发帖数: 157 | 1 以下选哪一个
class String {
char *s;
int length;
public:
String(const char *);
String();
/* add code here */
};
int main()
{
String s1 = "abc";
String s2 = "def";
strcmp(s1, s2);
getchar();
return(1);
}
Referring to the sample code above, which one of the following member
functions do you add at the comment in order to allow the strcmp(s1, s2)
statement to compile?
operator const char*() const { return s; }
char* const operator() const { return s; }
operator char* | M********5 发帖数: 715 | | M********5 发帖数: 715 | 3 首先,你要弄清楚这道题的考点。这道题的考点是conversion operator,参考c++
primer的527页。
conversion operator的很明显的一个特点是函数以operator开头,且没有返回值,依
据这一点,排除了三个选项。
第二个考点(本来不是太明显,在这题中),就是const究竟修饰什么,记住一点,
const放在*后面,修饰的就是指针,就是说指针不能再变。
第三个考点,strcmp的参数类型是什么?cstring!cstring又是什么?const char*!
所以这就是我的答案 |
|