CString strParameter = NULL;
时间: 2024-05-31 09:13:43 浏览: 7
在C++中,`CString`是MFC框架中提供的一个字符串类,用于方便地处理字符串。在这行代码中,`strParameter`是一个`CString`类型的变量,被赋值为`NULL`,表示该字符串为空。需要注意的是,在MFC中,`NULL`被定义为0,因此这行代码的实际作用是将`strParameter`字符串初始化为空字符串。
相关问题
CString str赋值
CString str的赋值可以通过以下方式实现:
1. 直接赋值:
```cpp
CString str = "Hello world!";
```
2. 使用赋值运算符:
```cpp
CString str;
str = "Hello world!";
```
3. 使用格式化字符串:
```cpp
CString str;
str.Format("Hello %s!", "world");
```
4. 从另一个CString对象进行赋值:
```cpp
CString str1 = "Hello world!";
CString str2 = str1;
```
修改以下代码# include <iostream> # include <string.h> using namespace std; class cstring{ char *ps; int n; public: cstring(){ n=0; ps=0;} cstring(char *cp) { n= strlen(cp)+1; ps= new char[ n]; strcpy(ps,cp);} ~ cstring(){delete [ ]ps; } cstring & operator= ( char *p) {delete []ps; n= strlen(p)+ 1; ps = new char[n]; strcpy(ps,p); return *this; } cstring & operator= ( cstring &s) {delete []ps; n=s.n; ps= new char[n]; strcpy(ps,s. ps); return *this;} cstring & operator + (char *p) {char *p1;int n1=n; p1= new char[n1]; strcpy(p1,ps); n+=strlen(p)+ 1; delete []ps; ps= new char[ n]; strcpy(ps, p1); delete []p1; strcat(ps,p); return *this;}; cstring & operator + ( cstring &s) {char *p1;int n1=n; p1 = new char[n1]; strcpy(p1 ,ps); n+=s.n; delete []ps; ps = new char[ n]; strcpy( ps,p1); delete []p1; strcat(ps,s.ps); return *this; } void disp(){ cout<< ps<<endl;} }; int main( ) {cstring str1 ,str2, str3(" string class "),str4; char *pc=" test"; cout< "pc="<< pc << endl; str1= pc; cout<<"str1="; str1. disp( ); str2=str3; cout<< "str2= "; str2. disp( ); cout << "tr3="; str3. disp(); str4=str3+pc; cout<< "str3+pc="; str4. disp(); str4=str1+str2;cout<<"str1+str2=";str4.disp(); }
修改后的代码如下:
#include <iostream>
#include <cstring>
using namespace std;
class cstring{
char *ps;
int n;
public:
cstring(){
n = 0;
ps = nullptr;
}
cstring(char *cp){
n = strlen(cp) + 1;
ps = new char[n];
strcpy(ps, cp);
}
~cstring(){
delete[] ps;
}
cstring &operator=(char *p){
delete[] ps;
n = strlen(p) + 1;
ps = new char[n];
strcpy(ps, p);
return *this;
}
cstring &operator=(cstring &s){
delete[] ps;
n = s.n;
ps = new char[n];
strcpy(ps, s.ps);
return *this;
}
cstring operator+(char *p){
cstring temp;
temp.n = n + strlen(p) + 1;
temp.ps = new char[temp.n];
strcpy(temp.ps, ps);
strcat(temp.ps, p);
return temp;
}
cstring operator+(cstring &s){
cstring temp;
temp.n = n + s.n;
temp.ps = new char[temp.n];
strcpy(temp.ps, ps);
strcat(temp.ps, s.ps);
return temp;
}
void disp(){
cout << ps << endl;
}
};
int main(){
cstring str1, str2, str3("string class"), str4;
char *pc = "test";
cout << "pc=" << pc << endl;
str1 = pc;
cout << "str1=";
str1.disp();
str2 = str3;
cout << "str2=";
str2.disp();
cout << "str3=";
str3.disp();
str4 = str3 + pc;
cout << "str3+pc=";
str4.disp();
str4 = str1 + str2;
cout << "str1+str2=";
str4.disp();
return 0;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)