1

How do I pass an object that has a pointer member inside it to a function? I want to be able to modify that objects member and have the original object in main have the modified value reflected upon it, Simple code I just want to know how to do this. I'm new. I made this code as simple as I code, I tried an overloaded assignment operator and a copy constructor but still no good. Can someone explain to me the process that happens when I call the function set(); and why I am getting an error

i want to use the set() function to change the objects name member to a different one

#include <iostream> #include <string> using namespace std; class student { public: void setName(string *nameVariable); void printName(){ cout << *name; } private: string *name; }; void student::setName(string *nameVariable) { name = nameVariable; } void set(student& student1); int main() { string *nameptr, name;//creates pointer and varible of type string nameptr = &name;//sets nameptr to the adress of name *nameptr = "Nick";//inderict access, assigns what nameptr is pointing to the value"nick" student student1; student1.setName(nameptr); student1.printName();//couts - Nick - set(student1); student1.printName();//should cout - Jorge - after modification however an error occurs cout << endl; system("pause"); } void set(student& rightStudent) { string *nameptr, name; nameptr = &name; *nameptr = "Jorge"; student localStudent; localStudent.setName(nameptr); rightStudent = localStudent; } 
5
  • Should I use a pointer...to an object? like student *studentptr = &student1? Commented Jul 31, 2015 at 5:12
  • localStudent is going out of scope, and that's what is causing your segmentation fault. Commented Jul 31, 2015 at 5:16
  • what do u mean out of scope Commented Jul 31, 2015 at 5:19
  • Do not use pointers and new unless you have to. As a beginner, you shouldn't have to. Also, read about the "Law of Three", which is essential info about the C++ object model. Commented Jul 31, 2015 at 5:38
  • Yeh, I gotcha on that. I just wanted to see how they work by using a simple example. Thanks Commented Jul 31, 2015 at 5:43

1 Answer 1

1

The main problem is that after the function set returns, student1 points to a dangling pointer. Inside the function, rightStudent points to the address of name. That address is invalid once the function returns. Hence, you are seeing undefined behavior.

The program is very brittle. You are storing a pointer but there isn't enough logic to make sure that the pointer remains valid as long as the object (of type student) is alive.

To make things simple, you should store an object instead of a pointer in student.

class student { public: void setName(string const& nameVariable); void printName(){ cout << name; } private: string name; }; 

Once you change that, the rest of your program becomes simpler.

void student::setName(string const& nameVariable) { name = nameVariable; } void set(student& student1); int main() { string name = "Nick"; student student1; student1.setName(name); student1.printName(); set(student1); student1.printName(); cout << endl; system("pause"); } void set(student& rightStudent) { string name = "Jorge"; rightStudent.setName(name); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I will study your comment, Yeh I just wanted to see how passing things like this works. I could have gone with your example.
Ahh so if I understand Correctly I would have to somehow make student1s name point to the address of localStudents name which in part will have the actual member variable name point to the location of name inside the function. Correct? I'm just spitting our rhymes here. Class member name-> main() name-> function() name, is what is supposed to happen. but isnt
The answer is "No". Your posted code does exactly that, and that is a problem.
Ahhhh.... the function terminates and everything inside gets closed(pointers and what not), thus the class object member is left pointing to nothing? which is why the print function doesn't work. I wonder if this would happen if it were a dynamic pointer.
The member is left pointing to an invalid object. That's why the function does not work. No, that would not happen if you allocated memory dynamically. But than, you'll need to make sure that you deallocate the memory that you allocated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.