剑指Offer之赋值运算符重载(题1)

简介:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
1 #include<stdio.h>                                                                                                                         
   2 #include< assert .h>
  
   class  MyString
   5 {
   public :
   7     MyString()
   8     {
   9         _data =  new  char [1];
  10         _data =  '\0' ;
  11     }
  12 
  13     MyString( char * str)
  14     {
  15          assert (str);
  16         _data =  new  char [ strlen (str) + 1];
  17          strcpy (_data, str);
  18     }
  19 
  20     MyString( const  MyString& string)
  21     {
  22          assert (string._data);
  23         _data =  new  char [ strlen (string._data) + 1];
  24          strcpy (_data, string._data);
  25     }
  26 
  27     ~MyString()
  28     {
  29          delete [] _data;
  30     }
  31 
  32     MyString& operator= ( const  MyString& string)
  33     {
  34          assert (string._data);
  35          if (* this  == string)
  36         {
  37              return  * this ;
  38         }
  39 
  40          delete [] _data;
  41         _data = NULL;
  42         _data =  new  char [ strlen (string._data) + 1];
  43          strcpy (_data, string._data);
  44          return  * this ;
  45     }
  46 
  47  protected :
  48      char * _str;
  49 }




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1 #include<stdio.h>
   2 #include< assert .h>
  
   class  MyString
   5 {
   public :
   7     MyString()
   8     {
   9         _data =  new  char [1];
  10         _data =  '\0' ;
  11     }
  12 
  13     MyString( char * str)
  14     {
  15          assert (str);
  16         _data =  new  char [ strlen (str) + 1];
  17          strcpy (_data, str);
  18     }
  19 
  20     MyString( const  MyString& string)
  21     {
  22          assert (string._data);
  23         _data =  new  char [ strlen (string._data) + 1];
  24          strcpy (_data, string._data);
  25     }
  26                                                                                                                                           
  27     ~MyString()
  28     {
  29          delete [] _data;
  30     }
  31 
  32     MyString& operator= ( const  MyString& string)
  33     {
  34          assert (string._data);
  35          if (* this  != string)
  36         {
  37             MyString tem(string);
  38 
  39              char * del = tem._data
  40             tem._data = _data;
  41             _data = del;
  42         }
  43          return  * this ;
  44     }
  45 
  46  protected :
  47      char * _str;
  48 }









本文转自 ye小灰灰  51CTO博客,原文链接:http://blog.51cto.com/10704527/1783629,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
C语言
C语言操作符详解(3)初始化和赋值操作符h
C语言操作符详解(3)初始化和赋值操作符h
|
3月前
|
存储 编译器 C语言
C++初阶类与对象(三):详解复制构造函数和运算符重载
C++初阶类与对象(三):详解复制构造函数和运算符重载
33 0
|
3月前
|
存储 安全 编译器
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)
这一篇文章是上一篇的续集(这里有上篇链接)前面我们讲了C语言的基础知识,也了解了一些数据结构,并且讲了有关C++的命名空间的一些知识点以及关于C++的缺省参数、函数重载,引用 和 内联函数。也相信大家都掌握的不错,接下来博主将会带领大家继续学习有关C++比较重要的知识点——类和对象(拷贝构造函数、赋值运算符重载、const成员、取地址及const取地址操作符重载)。
60 0
|
9月前
|
存储 编译器 C++
【C++初阶】类与对象:6大默认成员函数------拷贝构造和赋值运算符重载
【C++初阶】类与对象:6大默认成员函数------拷贝构造和赋值运算符重载
38 0
【C++初阶】类与对象:6大默认成员函数------拷贝构造和赋值运算符重载
|
4月前
|
编译器 C++
[C++ 从入门到精通] 12.重载运算符、赋值运算符重载、析构函数
[C++ 从入门到精通] 12.重载运算符、赋值运算符重载、析构函数
27 0
|
5月前
|
编译器 C++
36 C++ - 赋值(=)运算符重载
36 C++ - 赋值(=)运算符重载
20 0
|
8月前
|
算法 C++ 编译器
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)下
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)下
68 0
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)下
|
8月前
|
C语言 C++ 编译器
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)上
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)上
79 0
【C++入门到精通】C++入门 —— 类和对象(拷贝构造函数、赋值运算符重载、const成员函数)上
|
9月前
|
编译器 C++
【C++学习】类和对象 | 运算符重载 | 赋值运算符重载
【C++学习】类和对象 | 运算符重载 | 赋值运算符重载
54 0
|
10月前
剑指Offer - 面试题1:赋值运算符函数
剑指Offer - 面试题1:赋值运算符函数
30 0

热门文章

最新文章