这在 C++ 类和对象中指向此指针
this pointers in c++ classes and objects
如何在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();
}
相关文章:
- 为什么指针对象没有调用默认构造函数
- 如何访问双指针对象中的方法
- 如何使用智能指针对象执行成员函数指针
- 调用带有指针对象错误的指针变量
- 如何从模板类重载创建的指针对象上的运算符?
- C++指针对象的运算符重载
- 如何初始化数组指针对象
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- C++指针对象差异
- 涉及继承,指针对象的交易以及何时进行的问题
- 表达式必须在C 中具有指针对象类型
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 字符指针对象和相应的字符数组元素比较
- 如何在 c++ 中克隆为弱指针对象
- 指针对象使用CPP中的指针表达式进行比较
- Visual Studio中智能指针对象的自定义视图
- 使用指针 /对象 /模板参数正确构成
- 如果我确定只有一个线程一次处理指针/对象,则C/C 仍应使用同步
- 如何将值分配给指针对象 c++
- 更改智能指针对象,然后访问它