标题:总是到strlen这个关键字这里断点
只看楼主
新人学习
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:145
专家分:187
注 册:2017-10-26
结帖率:66.67%
 问题点数:0 回复次数:4 
总是到strlen这个关键字这里断点
#include<iostream>
using namespace std;
class Mystring
{
private:
    char *pt;
public:
    Mystring(){}
    Mystring(char *p )
    {
        pt = new char[strlen(p)+1];    //就在这里断点 纳闷/..
        int i;
        for (i = 0; i < strlen(p)+1; i++)
        {
            *(pt + i) = *(p + i);
        }
        *(pt + i) = *(p + i);
    }
    Mystring(Mystring &Object)
    {
        (*this) = Object;
    }
    Mystring& operator=(Mystring &Object)
    {
        pt = new char[strlen(Object.pt) + 1];
        int i;
        for (i = 0; i <= strlen(Object.pt); i++)
        {
            *(pt + i) = *(Object.pt + i);
        }
        *(pt + i) = *(Object.pt + i);
        return *this;
    }
    void Display()
    {
        cout << "pt = " << pt << endl;
    }


};
int main()
{
    Mystring Obj1, Obj2("I Love You");
    Obj1.Display();
    Obj2.Display();
   
    Mystring Obj3;
    Obj3 = Obj2;
    Obj3.Display();
    system("pause");
    return 0;
搜索更多相关主题的帖子: strlen 断点 char Object Display 
2018-01-20 13:07
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:0 
#include<iostream>
using namespace std;
class Mystring
{
private:
    char *pt;
public:
    Mystring(){pt=NULL;}
    Mystring(char *p )
    {
        pt = new char[strlen(p)+1];    //就在这里断点 纳闷/..
        int i;
        for (i = 0; i < strlen(p)+1; i++)
        {
            *(pt + i) = *(p + i);
        }
        *(pt + i) = *(p + i);
    }
    Mystring(Mystring &Object)
    {
        (*this) = Object;
    }
    Mystring& operator=(Mystring &Object)
    {
        pt = new char[strlen(Object.pt) + 1];
        int i;
        for (i = 0; i <= strlen(Object.pt); i++)
        {
            *(pt + i) = *(Object.pt + i);
        }
        *(pt + i) = *(Object.pt + i);
        return *this;
    }
    void Display()
    {
        if(pt!=NULL) cout << "pt = " << pt << endl;
    }


};
int main()
{
    Mystring Obj1, Obj2("I Love You");
    Obj1.Display();
    Obj2.Display();
   
    Mystring Obj3;
    Obj3 = Obj2;
    Obj3.Display();
    system("pause");
    return 0;
}
2018-01-21 14:01
新人学习
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:145
专家分:187
注 册:2017-10-26
得分:0 
我添加了一个析构函数 测试的DOS窗口关闭的时候要响下声音.
好像不正常
~Mystring()
{
    if(pt!=NULL)
       delete [] pt;
}


2018-01-21 19:39
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:0 
那对方括号去掉
2018-01-21 22:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
程序代码:
#include <iostream>
#include <cstring>

class Mystring
{
public:
    Mystring() try : pt_(new char[1])
    {
        *pt_ = '\0';
    }
    catch( ... ){ throw; }

    Mystring( const char* s ) try : pt_(new char[strlen(s)+1])
    {
        strcpy( pt_, s );
    }
    catch( ... ){ throw; }

    Mystring( const Mystring& s ) try : pt_(new char[strlen(s.pt_)+1])
    {
        strcpy( pt_, s.pt_ );
    }
    catch( ... ){ throw; }

    Mystring& operator=( const Mystring& s )
    {
        if( this != &s )
        {
            try {
                char* p = new char[strlen(s.pt_)+1];
                strcpy( p, s.pt_ );
                delete[] pt_;
                pt_ = p;
            }
            catch( ... ){ throw; }
        }
        return *this;
    }

private:
    char* pt_;

    friend std::ostream& operator<<( std::ostream& os, const Mystring& s )
    {
        return os << s.pt_;
    }
};

using namespace std;

int main( void )
{
    Mystring Obj1, Obj2("I Love You");
    cout << Obj1 << '\n'
         << Obj2 << endl;

    Mystring Obj3;
    Obj3 = Obj2;
    cout << Obj3 << endl;

    return 0;
}
2018-01-22 08:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484369-1-1.html




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

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