c++ - 使用 .Copy 执行复制构造函数的最佳方法?

标签 c++ string object copy-constructor

我需要帮助在我的面向对象程序中执行这个复制构造函数。结果应该是将字符串 1:Hello World 复制到字符串 2:This is a test

在我的.h 文件中:

void Copy(MyString& one);

在我的.cpp 文件中:

void MyString::Copy(MyString& one)
{
   one = String;
}

在我的main.cpp 文件中:

String1.Print();
cout  << endl;
String2.Print();
cout  << endl;      
String2.Copy(String1);      
String1.Print();       
cout  << endl;
String2.Print();
cout  << endl;

输出:

Hello World
This is a test
is a test
This is a test

应该是:

Hello World
This is a test
Hello World
Hello World

请向我解释我做错了什么?

这是我的整个 .cpp 文件:

MyString::MyString()

{

char temp[] = "Hello World";

        int counter(0);
        while(temp[counter] != '\0') {
                counter++;
        }
        Size = counter;
        String = new char [Size];
        for(int i=0; i < Size; i++)
                String[i] = temp[i];

}

MyString::MyString(char *message)

{

      int counter(0);

       while(message[counter] != '\0') {

      counter++;

   }

        Size = counter;

        String = new char [Size];


         for(int i=0; i < Size; i++)

           String[i] = message[i];

}

MyString::~MyString()

{

        delete [] String;

}

int MyString::Length()

{
              int counter(0);

              while(String[counter] != '\0')
             {
                     counter ++;
             }

                return (counter);
}

void MyString:: Set(int index, char b)

{

         if(String[index] == '\0')

          {
                   exit(0);
          }

         else

        {

                   String[index] = b;
          }


}

void MyString::Copy(MyString& one)

{

        one = String;


}

char MyString:: Get(int i)
{

             if( String[i] == '\0')
             {
                     exit(1);
             }
            else
            {

                    return String[i];

            }
}



void MyString::Print()

{

        for(int i=0; i < Size; i++)

            cout << String[i];

             cout << endl;


}

最佳答案

void Copy(MyString& one); 不是复制构造函数,但我想您所需要的只是一个执行复制的函数。

实现:

void MyString::Copy(MyString& one) {
   one = String;
}

这是获取 String,无论它是什么,大概是您的类内部存储,并将您传入的字符串分配给它...

这意味着 String2.Copy(String1) 就像做 String1 = String2.String。您想要的输出表明您想要将 String1 复制到 String2,但您的方向完全错误。

此外,该作业导致部分拷贝(“是一个测试”)这一事实意味着无论您如何进行该作业也被破坏了。你没有展示它的实现,所以我无法告诉你它是如何损坏的。


代码有很多问题。您可能应该与其他学生或助教一起复习。但是对于您的输出显示的具体问题,您应该看三件事。首先谷歌“三法则”。其次考虑当左侧是 MyString 对象而右侧是 char* 时“=”的含义。第三,一旦你弄清楚了数据将如何被复制,反转赋值的方向(例如,*this = out.String)这样你就可以在正确的方向上复制。

关于c++ - 使用 .Copy 执行复制构造函数的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255363/

相关文章:

c++ - STL unordered_map 序列化

c++ - PostgreSQL 独立应用程序

c++ - 从类定义中传递字符串指针

javascript - 遍历对象数组并获得新的对象数组

修改绑定(bind)方法及其类状态的 Python 装饰器

c++ - 具有自定义标量类型的特征值 : Matrix multiplication with custom type fails with `use of overloaded operator ' *' is ambiguous`

c++ - 在变量中存储异常的正确方法

C语言对字符串中的一个序列进行计数

regex - PostgreSQL regexp_replace() 只保留一个空格

java - 如何为任何类型的可比较对象实现compareTo