构造函数和desstructor问题 [英] Constructor and desstructor problem
问题描述
/ *程序对字符串进行操作* /
#include< iostream>
使用命名空间std;
class String
{
int len;
char * p;
public:
String()
{
cout<< 默认构造函数 <<这个<< endl;
len = 0;
p = NULL;
}
字符串(const char * s)
{
cout<< String constructor called << s<< endl;
len = strlen(s);
p = new char [len + 1];
strcpy(p,s); < br $>
}
String(const String& s)
{
cout<< 复制构造函数调用 << s.p<< endl ;;
len = s.len;
p = new char [len + 1];
strcpy(p,sp); < br $>
}
~String()
{
cout<< 删除 << p << " " <<这个<<结束;
删除p;
}
无效显示(无效)
{
cout<< p << " \t地址p =" << & p<< \ tobject this =" <<这个<< endl;
}
//朋友字符串运算符+(const String& s,const String& t);
字符串运算符+ (const String& t);
friend int operator< =(const String& s,const String& t);
friend void show(const String & s);
};
String String :: operator +(const String& t)
{
字符串临时;
cout<< 输入的运算符+() << & temp<< endl;
temp.len = len + t.len;
temp.p =新字符[temp.len + 1];
strcpy(temp.p,p);
strcat(temp.p,tp);
temp.display();
cout<< 退出运算符+() << & temp<< endl;
返回temp;
}
void show(const String& s)
{
cout<< sp;
}
main()
{
String s1(" Sidharth" ),s2(" Sweeya");
字符串s3;
//字符串s3 = s2 + s1;
s3 = s2 + s1;
s1.display();
s2.display();
s3.display ();
返回0;
}
输出程序:
名为Sidharth的字符串构造函数
名为Sweeya的字符串构造函数
默认构造函数0xbff06c50
default构造函数0xbff06c40
输入运算符+()0xbff06c40
Sweeya Sidharth地址p = 0xbff06c44对象this = 0xbff06c40
退出运算符+()0xbff06c40
删除Sweeya Sidharth 0xbff06c40
Sidharth地址p = 0xbff06c74对象this = 0xbff06c70
Sweeya地址p = 0xbff06c64对象this = 0xbff06c60
addr e = p = 0xbff06c54对象这= 0xbff06c50
删除0xbff06c50
***检测到glibc ***双免费或损坏(fasttop):0x09e83028 ***
流产(核心倾销)
如果我将主要代码更改为
main()
{
String s1(" Sidharth"),s2(" Sweeya");
// String s3;
字符串s3 = s2 + s1;
// s3 = s2 + s1;
s1 .display();
s2.display();
s3.display();
返回0;
}
输出:
名为Sidharth的字符串构造函数
String构造函数名为Sweeya
默认构造函数0xbffab2b0
输入运算符+()0xbffab2b0
Sweeya Sidharth地址p = 0xbffab2b4对象this = 0xbffab2b0
退出运算符+()0xbffab2b0
Sidharth地址o fp = 0xbffab2d4 object this = 0xbffab2d0
Sweeya地址p = 0xbffab2c4对象this = 0xbffab2c0
Sweeya Sidharth地址p = 0xbffab2b4对象this = 0xbffab2b0
删除Sweeya Sidharth 0xbffab2b0
删除Sweeya 0xbffab2c0
删除Sidharth 0xbffab2d0
第一种情况是在operator +函数中为析变量调用析构函数。但是在第二种情况下为什么没有被调用?
在第二种情况下,如果调用了复制构造函数,则p应该指向diff位置。为什么String s3 = s2 + s1不会调用复制构造函数?
请帮助我
/*Program to do manipulations on a string*/
#include <iostream>
using namespace std;
class String
{
int len;
char *p;
public:
String ()
{
cout << "default constructor " << this << endl;
len = 0;
p = NULL;
}
String (const char *s)
{
cout << "String constructor called " << s << endl;
len = strlen(s);
p = new char[len + 1];
strcpy(p, s);
}
String (const String &s)
{
cout << "Copy constructor invoked " << s.p << endl;;
len = s.len;
p = new char[len + 1];
strcpy(p, s.p);
}
~String ()
{
cout << "deleting " << p << " " << this << endl;
delete p;
}
void display(void)
{
cout << p << "\t address of p = " << &p << "\tobject this = " << this << endl;
}
// friend String operator + (const String &s, const String &t);
String operator + (const String &t);
friend int operator <= (const String &s, const String &t);
friend void show (const String &s);
};
String String :: operator + (const String &t)
{
String temp;
cout << "Entered operator+() " << &temp << endl;
temp.len = len + t.len;
temp.p = new char[temp.len + 1];
strcpy(temp.p, p);
strcat(temp.p, t.p);
temp.display();
cout << "Exiting operator+() " << &temp << endl;
return temp;
}
void show (const String &s)
{
cout << s.p;
}
main()
{
String s1("Sidharth"), s2("Sweeya ");
String s3;
//String s3 = s2 + s1;
s3 = s2 + s1;
s1.display();
s2.display();
s3.display();
return 0;
}
Output of the program:
String constructor called Sidharth
String constructor called Sweeya
default constructor 0xbff06c50
default constructor 0xbff06c40
Entered operator+() 0xbff06c40
Sweeya Sidharth address of p = 0xbff06c44 object this = 0xbff06c40
Exiting operator+() 0xbff06c40
deleting Sweeya Sidharth 0xbff06c40
Sidharth address of p = 0xbff06c74 object this = 0xbff06c70
Sweeya address of p = 0xbff06c64 object this = 0xbff06c60
address of p = 0xbff06c54 object this = 0xbff06c50
deleting 0xbff06c50
*** glibc detected *** double free or corruption (fasttop): 0x09e83028 ***
Aborted (core dumped)
If I change the code in the main to
main()
{
String s1("Sidharth"), s2("Sweeya ");
//String s3;
String s3 = s2 + s1;
//s3 = s2 + s1;
s1.display();
s2.display();
s3.display();
return 0;
}
Ouput:
String constructor called Sidharth
String constructor called Sweeya
default constructor 0xbffab2b0
Entered operator+() 0xbffab2b0
Sweeya Sidharth address of p = 0xbffab2b4 object this = 0xbffab2b0
Exiting operator+() 0xbffab2b0
Sidharth address of p = 0xbffab2d4 object this = 0xbffab2d0
Sweeya address of p = 0xbffab2c4 object this = 0xbffab2c0
Sweeya Sidharth address of p = 0xbffab2b4 object this = 0xbffab2b0
deleting Sweeya Sidharth 0xbffab2b0
deleting Sweeya 0xbffab2c0
deleting Sidharth 0xbffab2d0
In the first case the destructor is being called for the temp variable in the operator + function. But in the second case why is not called?
In the second case if copy constructor have been invoked then p should be pointing to a diff location. why does String s3 = s2+ s1 does not invoke a copy constructor?
Please help me
推荐答案
在函数operator +中,返回按值传递,这会在函数返回时立即删除对象。
In your function operator +, your returning "pass by value", this result in deletion of your object as soon as your function returns.
我明白了。但是为什么在第二种情况下没有调用析构函数?
i意思是当我说
字符串S3 = S1 + S2;
I understand that. But why is the destructor not being called in the second case?
i mean when i say
String S3 = S1 + S2;
我想当你说
字符串S3 = S2 + S1
将调用复制构造函数对于S3,将在operator + function中为temp obj调用析构函数
I thought when you say
String S3 = S2 + S1
A copy constructor will be invoked for S3 and a destructor will be invoked for the temp obj in the operator + function
这篇关于构造函数和desstructor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!