由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ 面试题
相关主题
1道brianbench 的题 c++请教一个C++问题
请教operator const char*() 的问题问一个精华区里的题目
攒人品之facebook电面面经leetcode上wild match
C++: 如何对const data member做assignment?被thank you的fb电面面经
G家电面,这回肯定挂了。附面经。longest common prefix 和 longest common substring
从Simplify Path问面试编程语言选择?菜鸟求救 请大家看看我的代码有没有问题
请问一道c++题目A challenging interview question: revert a string
C++ Q48: illegal operation (C33)一道题
相关话题的讨论汇总
话题: string话题: strcmpu话题: char话题: operator话题: const
进入JobHunting版参与讨论
1 (共1页)
l*****o
发帖数: 214
1
class String{
char* s;
int length;
public:
String(const char*);
String();
// add code here
};
int main() {
String s1 = "abc";
String s2 = "deg";
return strcmpu(s1, s2);
}
问 //add code here应该加什么code才能compile。。。
感觉上operator overloading但是怎么加有没有大牛能解释以下?
D***h
发帖数: 183
2
conversion operator

【在 l*****o 的大作中提到】
: class String{
: char* s;
: int length;
: public:
: String(const char*);
: String();
: // add code here
: };
: int main() {
: String s1 = "abc";

s*******e
发帖数: 93
3
我感觉好像可以加一个
static int strcmpu(String a, String b)
{
...
return ...;
}
但是在call strcmpu的时候好像应该是String::strcmpu
“conversion operator” 具体是怎么做呢?
K******g
发帖数: 1870
4
String& operator=(char *p)
{
s = p;
length = strlen(p);
return *this;
}
不知道对不对.

【在 l*****o 的大作中提到】
: class String{
: char* s;
: int length;
: public:
: String(const char*);
: String();
: // add code here
: };
: int main() {
: String s1 = "abc";

y*******o
发帖数: 6632
5
operator char*()
{
return s;
}

【在 l*****o 的大作中提到】
: class String{
: char* s;
: int length;
: public:
: String(const char*);
: String();
: // add code here
: };
: int main() {
: String s1 = "abc";

s*****n
发帖数: 5488
6
不对。s1, s2=调的是constructor.
缺的是friend function strcmpu.

【在 K******g 的大作中提到】
: String& operator=(char *p)
: {
: s = p;
: length = strlen(p);
: return *this;
: }
: 不知道对不对.

K******g
发帖数: 1870
7
为什么要*operator啊?
而且你这个operator有问题,应该是
char* operator*(String &str)
{
return str.s;
}

【在 y*******o 的大作中提到】
: operator char*()
: {
: return s;
: }

K******g
发帖数: 1870
8
那个调用的是assignment operator,如果你不定义一个assignment operator,编译会
出错,因为default那个不管用
至于那个strcmpu很显然需要定义啊,而其恩strcmpu不是成员函数

【在 s*****n 的大作中提到】
: 不对。s1, s2=调的是constructor.
: 缺的是friend function strcmpu.

s*****n
发帖数: 5488
9
it is a constructor. I can show you an exmple if you have the book
essential c++ by stadley B. Lippman.
If it is not for this red herring, this is just a stupid question.
compiling result of vc:
?? 1 error C3861: 搒trcmpu ??????

【在 K******g 的大作中提到】
: 那个调用的是assignment operator,如果你不定义一个assignment operator,编译会
: 出错,因为default那个不管用
: 至于那个strcmpu很显然需要定义啊,而其恩strcmpu不是成员函数

K******g
发帖数: 1870
10
Got it. Thanks for your reminding.

【在 s*****n 的大作中提到】
: it is a constructor. I can show you an exmple if you have the book
: essential c++ by stadley B. Lippman.
: If it is not for this red herring, this is just a stupid question.
: compiling result of vc:
: ?? 1 error C3861: 搒trcmpu ??????

E*****7
发帖数: 128
11
class String
{
public:
String(const char*);
String();
friend int strcmpu(const String& lhs, const String& rhs);
private:
char* s;
int len;
};
// Implementation for it to compile("Big Three" issue exists)
#include
class String
{
public:
String(const char* in_s)
{
if (in_s)
{
s = new char[strlen(in_s) + 1];
strcpy(s, in_s);
} else {
String();
}
}
String()
{
s = 0;
len = 0;
}
friend int strcmpu(const String& lhs, const String& rhs);
private:
char* s;
int len;
};
int strcmpu(const String& lhs, const String& rhs)
{
return strcmp(lhs.s, rhs.s);
}
int main()
{
String s1="abc";
String s2="def";
return strcmpu(s1, s2);
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
一道题G家电面,这回肯定挂了。附面经。
一个基本的string问题从Simplify Path问面试编程语言选择?
请帮忙看道题 c++ operator overload请问一道c++题目
问个c++题C++ Q48: illegal operation (C33)
1道brianbench 的题 c++请教一个C++问题
请教operator const char*() 的问题问一个精华区里的题目
攒人品之facebook电面面经leetcode上wild match
C++: 如何对const data member做assignment?被thank you的fb电面面经
相关话题的讨论汇总
话题: string话题: strcmpu话题: char话题: operator话题: const