如何在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
答案 0 :(得分:0)
第一件事是 您正在为数据类型字符串提供int类型的内存分配
字符串p是char *数据类型,因此您需要做的是 p =新字符[大小];
深层复制的第二件事,您需要 复制构造函数 或 *重载运算符= *
我可以告诉您如何使用它,但是最好自己学习本主题,以全面了解这些主题。
答案 1 :(得分:0)
我的看法:有关说明,请参见下文。
//#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();
}
查看结果
答案 2 :(得分:0)
#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();
}