【问题标题】:Passing an object to a method C++将对象传递给方法 C++
【发布时间】:2023-03-09 10:35:01
【问题描述】:

抱歉语法问题,不是母语人士。

所以我的任务是创建一个简单的程序,您应该能够在其中创建三个人,并输入他们的姓名、国家、职业和电话号码。您应该能够将保存的信息打印为电子表格。

所以我想出了这样一段代码:

#include <iostream>
#include <iomanip>

using namespace std;

class Person {
public:
    string surname;
    string name;
    string country;
    string occupation;
    string phone;

    // Set default value
    Person() {
        surname = "empty";
        name = "empty";
        country = "empty";
        occupation = "empty";
        phone = "empty";
    }

    // SET PERSON'S DATA
    void set_surname(string entered_surname) {
        surname = entered_surname;
    }

    void set_name(string entered_name) {
        name = entered_name;
    }

    void set_country(string entered_country) {
        country = entered_country;
    }

    void set_occupation(string entered_occupation) {
        occupation = entered_occupation;
    }

    void set_phone(string entered_phone) {
        phone = entered_phone;
    }

    // RETURN PERSONS DATA
    string get_surname() {
        return surname;
    }
    string get_name() {
        return name;
    }
    string get_country() {
        return country;
    }
    string get_occupation() {
        return occupation;
    }
    string get_phone() {
        return phone;
    }

};

void create_a_frankenstein(Person person) {
    string entered_data;
    cout << "Please, enter person's surname: \n";
    cin >> entered_data;
    person.set_surname(entered_data);

    cout << "Please, enter person's name: \n";
    cin >> entered_data;
    person.set_name(entered_data);

    cout << "Please, enter person's country: \n";
    cin >> entered_data;
    person.set_country(entered_data);

    cout << "Please, enter person's occupation: \n";
    cin >> entered_data;
    person.set_occupation(entered_data);

    cout << "Please, enter person's phone: \n";
    cin >> entered_data;
    person.set_phone(entered_data);
}

int main() {

    Person fst;
    Person snd;
    Person trd;
    Person group[3] = {fst, snd, trd};

    int people_created = 0;

    bool switch_on = true;

    while (switch_on) {
        cout << "What operation would you like to perform: \n";
        cout << "    1) Create new person \n";
        cout << "    2) Print out all of the available information \n";
        cout << "    3) Quit \n";


        //Get the number of operation to perform
        int operation;
        cout << "Please, enter a number: \n";
        cin >> operation;

        switch (operation) {
        //Option 1: create a person
        case 1:
            if (people_created == 3) {
                cout << "It is not possible to create more that three people";
            }

        else {
                create_a_frankenstein(group[people_created]);
                people_created++;
            }
            break;

        //Option 2: print out all of the available information
        case 2:
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_surname();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_name();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_country();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_occupation();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_phone();
            break;

        // Option 3: quit
        case 3:
            switch_on = false;
            break;
        }
    }

}

一切似乎都很好。除了它不会改变对象变量中的信息。

我的猜测是,当我将 Person 类型的对象传递给 create_a_frankenstein() 时,方法会创建对象的副本并开始使用副本而不更改原始对象中的任何内容。

我尝试过使用指针。我设法在更简单的例子上做我想做的事:

void first(int* a){
    for (int i = 0; i < 7; i++) {
        a[i] = a[i]+1;
    }
}

int main() {
    int a[7] = {0, 1, 2, 3, 4, 5, 6};
    for (int i=0; i<7; i++) {
        cout << a[i] << ' ';
    }
}

但是当我尝试在实验室中使用它时,它并不容易工作。

很高兴收到有关如何解决问题以及我应该复习或深入研究哪些主题的任何建议。提前感谢您!

【问题讨论】:

  • 您可能想看看参考资料。如果我对您的理解正确,他们会按照您的意愿行事。
  • a[i] = a[i]++ 无效并调用未定义行为。不要做作业,反正都会做的。
  • 您的函数按值接受 Person,这意味着您将获得调用者所拥有的任何内容的副本。然后你改变你的副本,而不触及函数之外的任何东西。正如 Jerome 所说,在 C++ 中使用引用是更好的风格,但指针也可以。
  • 或者,让create_a_frankenstein 实际上返回 Person 并完全放弃这个论点。
  • @JeromeReinländer,谢谢!这正是我所需要的。

标签: c++


【解决方案1】:

问题出在void create_a_frankenstein(Person person) 方法上。

您正在传递 Person 对象的副本。如果您想保留对对象所做的更改,请将其作为参考传递:void create_a_frankenstein(Person&amp; person)

注意:

  1. 不要使用数组。如果要存储对象序列,请改用std::vector

  2. 如果您将任何getter member function 定义为const,那就太好了 --> return_type getter_name(params) const { //body here}

【讨论】:

  • 感谢您的回答。这正是我需要的东西:) 如果您不介意,您能否扩展您的答案并告诉我为什么将矢量用于对象会更好?
  • @VanityAloe 这是因为 a)向量为您管理对象的生命周期,即遵循 RAII(尽管您的情况更简单,但很高兴知道。b)您实际上存储了 6 个对象。其中3个是组中的fst,snd,thrd和3个。也就是说,Person group[3] = {fst, snd, trd}; 语句创建了数组内对象的副本。
【解决方案2】:

尝试通过引用传递您的 Person 对象。您可以在这里找到更多信息:passing object by reference in C++。顺便说一句,在您的代码示例中,您没有调用名为“first”的函数。

【讨论】:

  • 感谢您的回答。这个链接很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多