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);
} |