如何在C++中使用位于相同类中的函数从构造函数更改this->value?

huangapple go评论75阅读模式
英文:

How to change this->value from constructor with a function inside the same class in C++?

问题

I have a class student and constructor for name and age but latter on student must be able to edit that info.
我有一个名为"Student"的类和用于姓名和年龄的构造函数,但之后学生必须能够编辑这些信息。

I tried following but edit info dont work:
我尝试了以下方法,但编辑信息不起作用:

class Student{
类 "Student" {
public:
公共:
string name;
字符串 名字;
int age;
整数 年龄;
int grades[3];
整数 成绩[3];
int assignments[4];
整数 任务[4];

Student(string name, int age){
学生(字符串 名字,整数 年龄){
this->name = name;
这->名字= 名字;
this->age = age;
这->年龄= 年龄;
}

void edit_info(){
编辑信息(){
string newName;
字符串 新名字;
int newAge;
整数 新年龄;

cout<<"Set new name:";
cout<<"设置新名字:";
cin>>newName;
cin>>新名字;
cout<<"Set new age:";
cout<<"设置新年龄:";
cin>>newAge;
cin>>新年龄;

this->name=newName;
这->名字= 新名字;
this->age=newAge;
这->年龄= 新年龄;

}

void show_info(){
显示信息(){
cout<<name<<"\n";
cout<<名字<<"\n";
cout<<age<<"\n";
cout<<年龄<<"\n";
}

Code when I chose which student to "play":

if(input == "studentOne"){
    如果(input == "studentOne") {
        cout<<"You are now student 1\n";
        cout<<"你现在是学生1\n";
        cout<<"What is your name:";
        cout<<"你叫什么名字:";
        cin >> s1_name;
        cin >> s1_age;
        Student student1(s1_name, s1_age);
        while(true){
            CMD(student1);
        }

and CMD function:

void CMD(Student student){
void CMD(学生 student) {
string command;
字符串 命令;
cout<<"Type your command(edit_info, submit_assignments, sitFortest, show_info):";
cout<<"输入命令(edit_info, submit_assignments, sitFortest, show_info):";
cin>>command;
cin>>命令;

if(command=="edit_info"){
    如果(命令=="edit_info") {
        student.edit_info();
        student.edit_info();
    }
}
else if(command=="show_info"){
    否则 if(命令=="show_info") {
        student.show_info();
        student.show_info();
    }
}

}

英文:

I have a class student and constructor for name and age but latter on student must be able to edit that info.
I tried following but edit info dont work:

class Student{
public:
    string name;
    int age;
    int grades[3];
    int assignments[4];

Student(string name, int age){
    this-&gt;name = name;
    this-&gt;age = age;
}

void edit_info(){
    string newName;
    int newAge;

    cout&lt;&lt;&quot;Set new name:&quot;;
    cin&gt;&gt;newName;
    cout&lt;&lt;&quot;Set new age:&quot;;
    cin&gt;&gt;newAge;

    this-&gt;name=newName;
    this-&gt;age=newAge;
    
}
void show_info(){
    cout&lt;&lt;name&lt;&lt;&quot;\n&quot;;
    cout&lt;&lt;age&lt;&lt;&quot;\n&quot;;
}

Code when I chose which student to "play":

if(input == &quot;studentOne&quot;){
        cout&lt;&lt;&quot;You are now student 1\n&quot;;
        cout&lt;&lt;&quot;What is your name:&quot;;
        cin &gt;&gt; s1_name;
        cout&lt;&lt;&quot;How old are you:&quot;;
        cin &gt;&gt; s1_age;
        Student student1(s1_name, s1_age);
        while(true){
            CMD(student1);
        }

and CMD function:

void CMD(Student student){
string command;
cout&lt;&lt;&quot;Type your command(edit_info, submit_assignments, sitFortest, show_info):&quot;;
cin&gt;&gt;command;

if(command==&quot;edit_info&quot;){
    student.edit_info();
}
else if(command==&quot;show_info&quot;){
    student.show_info();
}

}

答案1

得分: 0

为了初始化成员变量,请给成员分配一个明显的命名方案(有一些惯例,我习惯使用 m_ 前缀)。并查看这个链接:https://en.cppreference.com/w/cpp/language/constructor member initialization lists。

#include <vector>
#include <string>
#include <iostream>

class Student
{
public:
    Student(std::string name, unsigned int age) :
        m_name{ name }, 
        m_age{ age }
    {
        // 不要在函数体中初始化成员,使用成员初始化列表。
        //this->name = name;
        //this->age = age;
    }

    void edit_info() 
    {
        // 你可以直接输入成员。
        std::cout << "设置新的姓名:";
        std::cin >> m_name;
        std::cout << "设置新的年龄:";
        std::cin >> m_age;
    }

private: // 类的成员应该是私有的!
    std::string m_name;
    unsigned int m_age; // 你不希望年龄小于0,所以使用无符号整数
    std::vector<int> grades;
    std::vector<int> assignments;
};
英文:

To initialize member variables, give members a distinct naming scheme. (There are a few conventions around, I am used to prefixing with m_).
And have a look at this : https://en.cppreference.com/w/cpp/language/constructor member initialization lists.

#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

class Student
{
public:
    Student(std::string name, unsigned int age) :
        m_name{ name }, 
        m_age{ age }
    {
        // don&#39;t initialize members in body use member initialization list.
        //this-&gt;name = name;
        //this-&gt;age = age;
    }

    void edit_info() 
    {
        // you can directly input into members.
        std::cout &lt;&lt; &quot;Set new name:&quot;;
        std::cin &gt;&gt; m_name;
        std::cout &lt;&lt; &quot;Set new age:&quot;;
        std::cin &gt;&gt; m_age;
    }

private: // class members should be private!
    std::string m_name;
    unsigned int m_age; // you don&#39;t execpt age to be &lt; 0 so unsigned
    std::vector&lt;int&gt; grades;
    std::vector&lt;int&gt; assignments;
};

答案2

得分: 0

这段代码创建了一个新的 Student 对象,每当调用 CMD 时都会创建一个新的对象。CMD 函数中的对象 student 与调用函数中的对象 student1 相同。因此,对该对象的更改对调用函数中的对象没有影响。

这是因为默认情况下,C++ 使用值传递,这意味着在将对象作为参数传递时会进行复制。如果要避免发生这种情况,必须使用引用。

更改非常简单:

void CMD(Student& student){ // & 表示使用引用

额外的 &student 转换为对调用变量的引用,而不是独立的对象。现在对 student 的更改实际上会更改调用函数中的对象,而不是更改调用函数中对象的副本。

这是初学者常见的误解之一。有些语言使用引用传递,但在 C++ 中,默认使用值传递。

英文:

This code

Student student1(s1_name, s1_age);
while(true){
    CMD(student1);
}

void CMD(Student student){
    ...
}

creates a new Student object each time that CMD is called. The object student in the CMD function is not the same as the object student1 in the calling function. So changes to this object have no effect on the object in the calling function.

This is because by default C++ uses call by value, meaning that objects are copied when passed as parameters. If you want to avoid this happening then you must use references.

The change is very simple

void CMD(Student&amp; student){ // &amp; means use call by reference

The extra &amp; turns student into a reference to the calling variable, instead of a separate object. Now changes to student will actually change the object in the calling function, instead of changing a copy of the object in the calling function.

This is a common misunderstanding among beginners. Some languages do use call by reference, but in C++ the default is call by value.

huangapple
  • 本文由 发表于 2023年2月19日 16:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定