【问题标题】:Why does accessing members of class from outside gives accurate results while accessing it from member function does not work correctly?为什么从外部访问类的成员会给出准确的结果,而从成员函数访问它却不能正常工作?
【发布时间】:2021-09-27 05:41:00
【问题描述】:

以下代码使用成员函数save()将数据保存在文件中,并使用静态函数load()读回数据。

使用load()函数加载数据成功,但使用dump()函数打印结果出乎意料,提供图片链接供参考(@ 987654321@).

ma​​in() 访问类的成员会得到正确的结果。

谁能找出我的代码有什么问题。

    #include <bits/stdc++.h>
    using namespace std;
    
    class Person {
       public:
          int ID;
          char name[20];
          Person() { ID = -1; memset(name, '\0', 20); }
          void get_data();
          void dump();
    
          bool save();
          static Person *load(int id);
    };
    
    void Person::get_data() {
    
       cout << "Enter ID: ";
       cin >> ID;
    
       cout << "Enter name: ";
       cin.ignore();
       cin.get(name, 20);
    }
    
    void Person::dump() {
       cout << "ID  : " << this->ID << endl;
       cout << "Name: " << this->name << endl;
    }
    
    bool Person::save() {
    
       ofstream outfile("person.dat", ios::app | ios::binary);
       if(!outfile) { return false; }
    
       outfile.write((char*)this, sizeof(Person));
       outfile.close();
    
       return true;
    }
    
    Person *Person::load(int id) {
    
       Person tmp, *p;
    
       ifstream infile("person.dat", ios::binary);
       if(!infile) { return NULL; }
    
       while(true) {
    
          infile.read((char*)&tmp, sizeof(Person));
          if( infile.eof() ) break;
    
          if(tmp.ID == id) {
             infile.close();
             p = &tmp;
             return p;
          }
       }
    
       infile.close();
       return NULL;
    }
    
    int main(int argc, char *argv[]) {
       
       if(argc == 1) { // if statement to save data to file
          Person p;
          p.get_data(); // get user input[enter image description here][1]
          if( p.save() ) cout << "Save successfull\n"; // save data
          else cout << "Save aborted\n";
       }
    
       else { // if statement to load data from file
          Person *p;
          int id;
          cout << "Enter person ID to search: ";
          cin >> id;
    
          p = Person::load(id); // load data
    
          if( !p ) {
             cout << "Person not found\n";
          } else {
             printf("%d, %s\n", p->ID, p->name); // gives right result
             p->dump(); // gives wrong result
          }
       }
    
       return 0;
    }

[1]:

【问题讨论】:

  • 你应该给minimal reproducible example。此外,使用#include &lt;bits/stdc++.h&gt; 被视为bad practice
  • p = &amp;tmp;: tmp 一旦load() 返回就停止存在,所以p 指向你的主内存无效。从那时起,您就处于未定义行为领域,这意味着不可预测的结果。
  • 找不到骗子,但您正在返回指向临时对象的指针。加速你的编译器警告。

标签: c++ file class object pointers


【解决方案1】:

load 函数返回一个指向一旦load 返回就不再存在的对象的指针。在该对象不再存在后尝试访问它是错误的,但main 会这样做。

修正错误。

【讨论】:

  • 是的,你是对的,这是一个无效的内存访问,因此出现了不可预测的行为。这是我的第一个 stackoverflow 问题,很抱歉在我的代码中使用了不良做法。感谢您的帮助。
猜你喜欢
  • 2011-05-17
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多