为什么初始化第二个对象会修改我的第一个对象?

时间:2017-01-07 20:14:59

标签: c++ class object

我有一个类IStream2:

class IStream2 {
   private:
      char* fn;
   public:
      IStream2(char* filename);
      char* get_filename();
};


IStream2::IStream2(char *filename) {
   strcpy(fn, filename);
}
char * IStream2::get_filename() {
   return fn;
}

以下是主要代码:

vector<IStream2> istreams;

char fn[] = "file1.txt";
IStream2 reader2 = IStream2(fn);
istreams.push_back(reader2);

char fn2[] = "file2.txt";
IStream2 reader3 = IStream2(fn2);
istreams.push_back(reader3);
cout << istreams[0].get_filename() << endl;

它打印file2.txt但我期望file1.txt。 我知道我应该使用字符串,但我想解决这个问题。

1 个答案:

答案 0 :(得分:2)

IStream2::IStream2(char *filename) {
   strcpy(fn, filename);
}

fn分配无存储空间。 strcpy(fn, filename);调用未定义的行为写入任何存储fn点,然后关闭所有投注。该计划可以做任何事情。

正确的答案是使用std::string

class IStream2 {
private:
    std::string fn;
public:
    IStream2(const char* filename); // note const. if not modifying a passed rference, 
                                    // mark it const. The compiler can make optimizations 
                                    // and can catch mistakes for you
                                    // also the function can now receive string literals
    const char* get_filename(); // another const this is because a string won't 
                                // easily give you a non const pointer
}; <-- note the added ;

IStream2::IStream2(const char *filename): fn(filename) {
}
const char * IStream2::get_filename() {
   return fn.c_str(); // get the char array from the string
}

但是我怀疑这是用C课写C的练习,所以回到我们去的石头时代。这是一项很多工作,因为我们必须自己管理所有内存。例如,我们需要遵守三法则。 What is The Rule of Three?

叹息。

class IStream2 {
private:
    char* fn;
public:
    IStream2(const char* filename); // note const char *
    ~IStream2(); // need destructor to clean up fn. This means we need to
                 // comply with the Rule of Three
    IStream2(const IStream2 & src); // copy constructor
    IStream2 & operator=(IStream2 src); // assignment operator
    char* get_filename(); // Note: by returning a non const pointer here we
                          // allow the caller to tamper with the contents of 
                          // fn and even delete it. This defeats the point 
                          // of declaring fn private, so avoid doing this.
};

IStream2::IStream2(const char *filename) {
   fn = new char[strlen(filename) +1]; // allocate storage. 
                                       // The +1 is to hold the string's NULL terminator
   strcpy(fn, filename);
}
// implement copy constructor
IStream2::IStream2(const IStream2 & src) {
   fn = new char[strlen(src.fn) +1];
   strcpy(fn, src.fn);
}
// implement destructor
IStream2::~IStream2()
{
    delete[] fn;
}
// implement assignment operator. Using Copy And Swap Idiom 
IStream2 & IStream2::operator=(IStream2 src)
{
    std::swap(fn, src.fn);
    return *this;
}
char * IStream2::get_filename() {
   return fn;
}


int main()
{
    vector<IStream2> istreams;

    const char* fn = "file1.txt"; // note const char *. string literals may be
                                  // read-only memory and can't be changed
    IStream2 reader2 = IStream2(fn);
    istreams.push_back(reader2);

    const char* fn2 = "file2.txt"; // and again const char *
    IStream2 reader3 = IStream2(fn2);
    istreams.push_back(reader3);
    cout << istreams[0].get_filename() << endl;
    return 0;
}

由于我们正在与恐龙搏斗,我不会为Rule of Five而烦恼并且移动操作,但是看到以错误的方式做这件事会有多烦人?

更多关于Copy And Swap Idiom