注册 登录
编程论坛 C++教室

总是到strlen这个关键字这里断点

新人学习 发布于 2018-01-20 13:07, 2008 次点击
#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;
4 回复
#2
yangfrancis2018-01-21 14:01
#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;
}
#3
新人学习2018-01-21 19:39
我添加了一个析构函数 测试的DOS窗口关闭的时候要响下声音.
好像不正常
~Mystring()
{
    if(pt!=NULL)
       delete [] pt;
}


#4
yangfrancis2018-01-21 22:47
那对方括号去掉
#5
rjsp2018-01-22 08:40
程序代码:
#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;
}
1