C++中传递Person对象至方法后无法修改原对象的问题求助
Hey there! You’re totally right about the value passing issue—when you pass a Person object by value, the function works on a copy instead of the original. Let’s fix this with clear solutions and break down the key concepts you need to learn.
Two Working Solutions
1. Pass by Reference (Recommended for This Scenario)
References are aliases for your original object, so any changes in the function directly affect the original. This is cleaner than pointers for simple modification tasks.
Here’s how to implement it:
First, define your Person class (with a constructor for easy initialization):
#include <iostream> #include <string> #include <iomanip> using namespace std; class Person { public: string name; string country; string occupation; string phone; Person(string n = "", string c = "", string o = "", string p = "") : name(n), country(c), occupation(o), phone(p) {} };
Then rewrite your create_a_frankenstein function to take a reference:
void create_a_frankenstein(Person& person) { // All changes here modify the original object person.name = "Frankenstein"; person.country = "Transylvania"; person.occupation = "Mad Scientist's Creation"; person.phone = "N/A"; }
Finally, in your main function, create objects, call the function, and output the table:
int main() { Person p1("Alice", "USA", "Engineer", "123-456-7890"); Person p2("Bob", "Canada", "Teacher", "987-654-3210"); Person p3("Charlie", "UK", "Doctor", "456-789-0123"); // Modify the third person create_a_frankenstein(p3); // Print in table format cout << left << setw(15) << "Name" << setw(15) << "Country" << setw(20) << "Occupation" << setw(15) << "Phone" << endl; cout << string(65, '-') << endl; cout << left << setw(15) << p1.name << setw(15) << p1.country << setw(20) << p1.occupation << setw(15) << p1.phone << endl; cout << left << setw(15) << p2.name << setw(15) << p2.country << setw(20) << p2.occupation << setw(15) << p2.phone << endl; cout << left << setw(15) << p3.name << setw(15) << p3.country << setw(20) << p3.occupation << setw(15) << p3.phone << endl; return 0; }
2. Pass by Pointer (If You Prefer Using Pointers)
If you want to use pointers, the common mistakes are forgetting to pass the object’s address or using . instead of -> to access members. Here’s the correct implementation:
void create_a_frankenstein(Person* person) { // Always check for null pointers to avoid crashes if (person != nullptr) { person->name = "Frankenstein"; person->country = "Transylvania"; person->occupation = "Mad Scientist's Creation"; person->phone = "N/A"; } } // Call the function like this in main: create_a_frankenstein(&p3); // & gets the address of p3
Key Concepts to Deepen Your Learning
- Value vs. Reference vs. Pointer Passing: Understand how each method handles object copies and memory. Value passing makes a duplicate, reference/pointer pass access the original.
- C++ References: Learn that references are immutable aliases (can’t be re-assigned to another object) and why they’re safer than pointers for basic modification tasks.
- Pointer Basics: Master how pointers store memory addresses, how to use
->to access class members, and how to avoid null pointer errors. - Copy Constructors: When you pass by value, C++ uses the copy constructor to create the object copy. Learning this helps you grasp why modifications don’t affect the original.
I bet your earlier pointer attempt had a small oversight—like missing the & when passing the object, or using . instead of ->! Double-check those details if you stick with pointers.
内容的提问来源于stack exchange,提问作者Vanity Aloe

