这在 C++ 类和对象中指向此指针

this pointers in c++ classes and objects

本文关键字:指针 对象 C++ 这在      更新时间:2023-10-16

如何在C++中使用深拷贝打印字符串?

#include <iostream.h>

using namespace std;
class demo
{
string a;
string *p;
public: 
demo() 
{
a=0;
p = new int;               // DEFAULT CONSTRUCTORS
*p = NULL;
} 
demo ( const string *q )
{
p= new int;
*p=q;
}

demo (demo &r) {
a= r.a;
p= new int; 
*p= *(r.p);
}
~demo ()  {
delete p;  
}
void show () {
cout << a;
} 
void change () {
s3.a=s2.a;
}
};
int main () {
demo s1;
demo s2("Hello");
demo s3(s2);
s1.show();
s2.show();
s3.show();
s2.change("Java");
s2.show();
s3.show();
}

期望输出:

HelloHelloJavaHello

我的看法:有关解释,请参见下文。

//#include <iostream.h> MODIFIED: Modern library headers do not have extension.
#include <iostream>
using namespace std; 
class demo {
string a;
// string *p; DELETED: Not sure what was the purpose of this.
public: 
demo() 
{
//a=0;         DELETED: std::string already have a sane initialization.
//                      and assigning it to 0 looks like a bad idea to me.
//                      (it will be taken as a char * nullptr)
//p = new int; DELETED: We removed p. Also p was a pointer to string
//*p = NULL;   DELETED: we removed p (modern C++ will use nullptr)
} 
demo ( const string *q ): a(*q) // MODIFIED: use initializers
{            
//p= new int; DELETED: we don't have p.
//*p=q;       DELETED: we don't have p.
//However, see below for what you seem to need.
//given your example program.
//Also this constructor can have a problem if a
//nullptr is used as parameter.
}
demo (const std::string &_a):a(_a) { 
// NEW: to support the constructor from string
}            
demo (demo &r):a(r.a) {
//a= r.a;      MOVED as initializer.
//p= new int;  DELETED: we don't have a p.
//*p= *(r.p);  DELETED: we don't have a p.
}
~demo ()  {
//delete p;  //No need
}
void show () {
cout << a;  
} 
void change (const std::string &_a) { // MODIFIED: added signature.
//s3.a=s2.a; MODIFIED: properly assign input value to member
a = _a;
}
};
int main () {
demo s1;
demo s2("Hello");
demo s3(s2);
s1.show();
s2.show();
s3.show();
s2.change("Java");
s2.show();
s3.show();
}

在 https://ideone.com/0wtq3l 查看结果

第一件事是 您正在为数据类型字符串提供 int 类型的内存分配

字符串 p 是 char* 数据类型,所以你需要做的是 p = 新字符[大小];

对于深度复制的第二件事,您需要复制构造函数*重载运算符 = *

我可以告诉如何使用它,但最好自己学习这些主题以获得对这些主题的完整了解。

#include <iostream>
using namespace std; 
class demo {
string a;
// string *p; DELETED: Not sure what was the purpose of this.
public: 
demo() 
{
//a=0;         DELETED: std::string already have a sane initialization.
//                      and assigning it to 0 looks like a bad idea to me.
//                      (it will be taken as a char * nullptr)
//p = new int; DELETED: We removed p. Also p was a pointer to string
//*p = NULL;   DELETED: we removed p (modern C++ will use nullptr)
} 
demo ( const string *q ): a(*q) // MODIFIED: use initializers
{
//p= new int; DELETED: we don't have p.
//*p=q;       DELETED: we don't have p.
}

demo (demo &r):a(r.a) { 
//a= r.a;      MOVED as initializer.
//p= new int;  DELETED: we don't have a p.
//*p= *(r.p);  DELETED: we don't have a p.
}
demo (const std::string &_a):a(_a) { 
// NEW: to support the constructor from string
}        
~demo ()  {
//delete p;  //No need
}
void show () {
cout << a;  
}           void change (const std::string &_a) { // MODIFIED: added signature.
//s3.a=s2.a; MODIFIED: properly assign input value to member
a = _a;
}
};
int main () {
demo s1;
demo s2("Hello");
demo s3(s2);                s1.show();
s2.show();
s3.show();
s2.change("Java");
s2.show();
s3.show();
}