随笔 - 477  文章 - 813  trackbacks - 0
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678


子曾经曰过:编程无他,唯手熟尔!
feedsky
抓虾
pageflakes
Rojo
狗狗
google reader
bloglines
my yahoo
newsgator
netvibes
鲜果


Locations of visitors to this page

常用链接

留言簿(66)

随笔分类(616)

随笔档案(477)

相册

BCB

Game Industry

OGRE

other

Programmers

Qt

WOW Stuff

搜索

  •  

积分与排名

  • 积分 - 276878
  • 排名 - 5

最新随笔

最新评论

阅读排行榜

评论排行榜

60天内阅读排行

/* 这类看着熟悉不?林锐大哥书上的,嘿嘿,好多公司都考啊! 大家给挑挑错误,改进一下.顺便解答一下我的疑问 */
 1
 class String
 2 {
 3 public:
 4     String(const char *str = NULL);    // 普通构造函数
 5     String(const String &other);        // 拷贝构造函数
 6     ~String(void);                        // 析构函数
 7     String & operator =(const String &other);    // 赋值函数
 8     const char* operator()(voidconst;
 9 private:
10     char      *m_data;                // 用于保存字符串
11 };
12 
13 
14 String::~String(void)
15 {
16     delete[] m_data;
17 }
18 
19 String::String(const char* str)
20 {
21     if(NULL == str)
22     {
23         // 不能让新对象的m_data为NULL
24         m_data = new char[1];
25         *m_data = '\0';
26     }    
27     else
28     {
29         int length = strlen(str);
30         m_data = new char[length + 1];
31         strcpy(m_data, str);
32     }
33 }
34 
35 String::String(const String& other) /* 因为是新构造一个对象,所以不需要像operator=一样判断自赋值及释放原来的内存 */
36 {
37     int length = strlen(other.m_data);
38     m_data = new char[length + 1];
39     strcpy(m_data, other.m_data);
40 }
41 
42 String& String::operator =(const String& other)
43 {
44     // 判断自赋值
45     if(this == &other)
46         return *this;
47     // 释放原来的内存
48     delete [] m_data;
49 
50     int length = strlen(other.m_data); /* 虽然m_data是private,但因为是在String的成员函数里,故没问题 */
51     m_data = new char[length + 1];
52     strcpy(m_data, other.m_data);
53 
54     return *this;
55 
56 }
57 
58 const char * String::operator()() const
59 {
60     return (const char*)m_data;
61 }
62
63 int _tmain(int argc, _TCHAR* argv[])
64 {
65     String str("hello");
66     String str1 = str; /* 这儿调用的是copy constructor,非 operator=, why? */
67     printf("%s\n", str1);
68 
69     const char* str2 = str();
70     printf("%s\n", str2);
71 
72     return 0;
73 }
posted on 2006-07-13 22:21 七星重剑 阅读(3006) 评论(15)  编辑 收藏 引用 所属分类: 面试笔试

FeedBack:
# re: 关于String类的笔试题及解答 2006-07-14 09:24 沐枫
operator = 不是异常安全的。
string不需要operator()操作。

String str1 = str;//调用拷贝构造函数
这个听说是C++标准规定的。  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-15 02:00 阿来
@沐枫
>operator = 不是异常安全的。

大哥再说得清楚点,呵呵。
>string不需要operator()操作。

我去征途笔试的时候,就有这个operator().面试我的那大哥说,other.m_data是不行的,因为m_data是private的,我说但是这是在String类的成员函数内,other也是String对象。他仍然坚持并让我回家的时候测试一下,我犹豫了,说那就用other()代替other.m_data。我回来测试了一下other.m_data没问题。  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-18 09:31 有风
下面是我在VC里编译上面的源码时提示的错误:

--------------------Configuration: String - Win32 Debug--------------------
Compiling...
String.cpp
D:\Chandi\C++学习\String.cpp(4) : error C2065: 'NULL' : undeclared identifier
D:\Chandi\C++学习\String.cpp(4) : error C2440: 'default argument' : cannot convert from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\Chandi\C++学习\String.cpp(21) : error C2446: '==' : no conversion from 'const char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\Chandi\C++学习\String.cpp(21) : error C2040: '==' : 'int' differs in levels of indirection from 'const char *'
D:\Chandi\C++学习\String.cpp(29) : error C2065: 'strlen' : undeclared identifier
D:\Chandi\C++学习\String.cpp(31) : error C2065: 'strcpy' : undeclared identifier
D:\Chandi\C++学习\String.cpp(63) : error C2061: syntax error : identifier '_TCHAR'
D:\Chandi\C++学习\String.cpp(67) : error C2065: 'printf' : undeclared identifier
Error executing cl.exe.

String.obj - 8 error(s), 0 warning(s)
  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-18 16:22 阿来
@有风
你是在VC6里面的编译得吧?

我用的是 visual studio 2003  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-19 15:36 有风
恩。
加了下面两句后,还是用两个ERROR。
#include<iostream.h>
#include<string.h>


--------------------Configuration: String - Win32 Debug--------------------
Compiling...
String.cpp
String.cpp(63) : error C2061: syntax error : identifier '_TCHAR'
String.cpp(67) : error C2065: 'printf' : undeclared identifier
Error executing cl.exe.  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-23 15:11 阿来
@有风
你加上 #include <stdio.h> // printf()是不是包含在这个头文件中?

把 int _tmain(int argc, _TCHAR* argv[]) 换成 int main() 好了

你再试试  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-28 11:29 有风
@阿来

65 String str("hello");
66 String str1 = str; /* 这儿调用的是copy constructor,非operator=, why? */

关于你上面的疑问,你试着读读林锐大哥的《高质量C++编程指南》里的这段话:

拷贝构造函数和赋值函数非常容易混淆,常导致错写、错用。拷贝构造函数是在对象被创建时调用的,而赋值函数只能被已经存在了的对象调用。以下程序中,第三个语句和第四个语句很相似,你分得清楚哪个调用了拷贝构造函数,哪个调用了赋值函数吗?
String a(“hello”);
String b(“world”);
String c = a; // 调用了拷贝构造函数,最好写成 c(a);
c = b; // 调用了赋值函数
本例中第三个语句的风格较差,宜改写成String c(a) 以区别于第四个语句。  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-28 11:37 有风
另外:
关于String类的赋值函数:
String & operator =(const String &other); // 赋值函数
为什么在《高质量C++编程指南》里,他是这样写的:
String & operate =(const String &other); // 赋值函数

operate跟operator有什么区别?  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-07-29 14:02 阿来
@有风
大哥解答了我好久的疑问啊,感激涕零!
> operate跟operator有什么区别?

《高质量》上应该是错把operator写成operate了吧?operator 是运算符的意思  回复  更多评论
  
# re: 关于String类的笔试题及解答 2006-10-27 10:23 沐枫
operator=存在一个异常安全问题。
假设,执行到new char[]时,发生异常,这时,原有的内容已经被delete掉了。这时,operator=扔出异常,同时,原有的对象已经不能用了。  回复  更多评论
  
# re: 关于String类的笔试题及解答 2007-05-23 21:39 秒大刀
当时公司来西安笔试也考这题目了  回复  更多评论
  
# re: 关于String类的笔试题及解答[未登录] 2007-05-23 22:45 阿来
@秒大刀
晕,你小伙儿怎么知道我博客的?  回复  更多评论
  
# re: 关于String类的笔试题及解答[未登录] 2007-09-14 15:12 Jeff
"不能让新对象的m_data为NULL", 为什么?  回复  更多评论
  
# re: 关于String类的笔试题及解答 2007-09-14 23:58 重剑
@Jeff
能为NULL吗?  回复  更多评论
  
# re: 关于String类的笔试题及解答 2008-09-06 13:05 秒大刀
字符串永远都是C++中的热门话题  回复  更多评论
  


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
.NET频道  博客园社区  闪存
网站导航: