| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4944 人关注过本帖
标题:请问这里的析构函数应该怎么写?
只看楼主 加入收藏
小虫笨
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:10
注 册:2018-5-30
收藏
 问题点数:0 回复次数:4 
请问这里的析构函数应该怎么写?
这里的析构函数应该怎么写?用来释放new过的内存

程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
class str
{
public:
    str(char *name);
    void print(str &str);
    int Size(str &str);
    static void exchange(str &str1, str &str2);
    static void Connect(str &str1, str &str2);
    ~str();
protected:
    char *name;
    int size = 0;
};
str::str(char *name)
{
    this->name = new char[strlen(name) + 1];
    this->name = name;
    this->size = strlen(name) + 1;
}
void str::print(str &str)
{
    for (int i = 0; i < str.size; i++)
    {
        cout << name[i];
    }
    cout << endl;
}
int str::Size(str &str)
{
    return str.size;
}
void str::exchange(str &str1, str &str2)
{
    if (str1.Size(str1) < str2.Size(str2))
    {
        str1.name = new char[str2.Size(str2) - 1];
        memset(str1.name, 0, str2.Size(str2) - 1);
        strcpy(str1.name, str2.name);
        str1.size = str2.Size(str2) - 1;
    }
    str1.name = str2.name;
}
void str::Connect(str &str1, str &str2)
{
    char *str3 = new char[(str1.Size(str1) + str2.Size(str2)) - 1];
    memset(str3, 0, (str1.Size(str1) + str2.Size(str2)) - 1);
    strcat(str3, str1.name);
    strcat(str3, str2.name);
    str1.name = new char[(str1.Size(str1) + str2.Size(str2)) - 1];
    memset(str1.name, 0, (str1.Size(str1) + str2.Size(str2)) - 1);
    str1.name = str3;
    str1.size = (str1.Size(str1) + str2.Size(str2)) - 1;
}
str::~str()
{

    cout << "析构函数" << endl;
}
int main()
{
    str mystr = "I LOVE YOU";
    cout << "对象大小:" << mystr.Size(mystr) << endl;
    cout << "对象为:";
    mystr.print(mystr);
    cout << endl;
    str mystr1 = "I hate you";
    str mystr2 = "I miss you123";
    cout << "新对象1为:";
    mystr1.print(mystr1);
    cout << "新对象2为:";
    mystr2.print(mystr2);
    cout << "对象复制操作" << endl;
    str::exchange(mystr1, mystr2);
    cout << "复制后对象1为:";
    mystr1.print(mystr1);
    cout << "复制后对象2为:";
    mystr2.print(mystr2);
    str mystr3 = "I hate you";
    str mystr4 = "I miss you";
    cout << "连接操作:";
    str::Connect(mystr3, mystr4);
    mystr3.print(mystr3);
    system("pause");
    return 0;
}

搜索更多相关主题的帖子: str name print Size cout 
2018-07-01 22:55
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
delete[] name;
为什么不用 string name? 都不需要手动管理内存
2018-07-01 23:50
小虫笨
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:10
注 册:2018-5-30
收藏
得分:0 
回复 2楼 Jonny0201
老师给的作业。要自己试着简单的写一下
2018-07-02 00:17
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
错误实在太多,建议你回炉再造
1. #include <string.h>
2. using namespace std;
3. const、const
4. size_t
5. 有需要分配资源的构造函数,那必然得有拷贝构造函数和赋值函数
6. 实现里面的多错逻辑错误

我随手改了改,错误难免,仅供参考
程序代码:
#include <iostream>
#include <cstring>

class str
{
public:
    str() noexcept;
    str( const char* s );
    str( const str& s );
    str( str&& s ) noexcept;
    ~str();
    str& operator=( const char* s );
    str& operator=( const str& s );
    str& operator=( str&& s ) noexcept;

    size_t size() const noexcept;
    void swap( str& s ) noexcept;
    friend str operator+( const char* lhs, const str& rhs );
    friend str operator+( const str& lhs, const char* rhs );
    friend str operator+( const str& lhs, const str& rhs );
    friend std::ostream& operator<<( std::ostream& os, const str& s );

protected:
    char* data_;
    int size_;
};

inline str::str() noexcept : data_(), size_()
{
}
inline size_t str::size() const noexcept
{
    return size_;
}
inline str::~str()
{
    delete[] data_;
}

str::str( const char* s ) : size_(), data_()
{
    size_ = strlen(s);
    data_ = new char[size_+1];
    memcpy( data_, s, (size_+1)*sizeof(*data_) );
}
str::str( const str& s ) : size_(), data_()
{
    size_ = s.size_;
    data_ = new char[size_+1];
    memcpy( data_, s.data_, (size_+1)*sizeof(*data_) );
}
str::str( str&& s ) noexcept : data_(s.data_), size_(s.size_)
{
    s.data_ = nullptr;
    s.size_ = 0;
}
str& str::operator=( const char* s )
{
    str tmp(s);
    swap( tmp );
    return *this;
}
str& str::operator=( const str& s )
{
    str tmp(s);
    swap( tmp );
    return *this;
}
str& str::operator=( str&& s ) noexcept
{
    swap( s );
    return *this;
}

void str::swap( str& s ) noexcept
{
    std::swap( data_, s.data_ );
    std::swap( size_, s.size_ );
}

str operator+( const str& lhs, const str& rhs )
{
    str r;
    r.size_ = lhs.size_ + rhs.size_;
    r.data_ = new char[r.size_+1];
    memcpy( r.data_, lhs.data_, lhs.size_*sizeof(*r.data_) );
    memcpy( r.data_+lhs.size_, rhs.data_, (rhs.size_+1)*sizeof(*r.data_) );
    return r;
}

std::ostream& operator<<( std::ostream& os, const str& s )
{
    return os.write( s.data_, s.size_ );
}

using namespace std;

int main( void )
{
    str mystr = "I LOVE YOU";
    cout << "字符长度 " << mystr.size() << '\n';
    cout << "值为 " << mystr << '\n';
    cout << endl;

    str mystr1 = "I hate you";
    str mystr2 = "I miss you123";
    cout << "新对象1为 " << mystr1 << '\n';
    cout << "新对象2为 " << mystr2 << '\n';
    cout << endl;

    cout << "对象交换操作:\n";
    mystr1.swap( mystr2 );
    cout << "交换后对象1为 " << mystr1 << '\n';
    cout << "交换后对象2为 " << mystr2 << '\n';
    cout << endl;

    cout << "连接操作:\n";
    str mystr3 = "I hate you";
    str mystr4 = "I miss you";
    cout << (mystr3+mystr4) << '\n';
    cout << endl;

    return 0;
}


2018-07-02 09:47
小虫笨
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:10
注 册:2018-5-30
收藏
得分:0 
回复 4楼 rjsp
十分感谢。重载老师提过,但是还没有上过课。。暂时有些不懂,就当先预习下后面的课程。,等重载学了,在仔细研究下。
2018-07-02 14:56
快速回复:请问这里的析构函数应该怎么写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027594 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved