Sign in to follow this  

Returning a data member of a pointer

This topic is 2087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just messing around in C++, and I'm using pointers at the moment. They can be a bit tricky. Right now, I'm trying to return a data member of a pointer to an object, and something is wrong. Can someone help me point out the issue in this code?

[code]#include <iostream>
#include <string>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& os, const Person& aPerson);
public:
Person(const string name, const string gender, int age);
~Person();
string m_name;
string m_gender;
void setFather(Person *person1, Person *person2);
void setMother(Person *person1, Person *person2);
private:
int m_age;
Person *m_father;
Person *m_mother;
};
Person::Person(const string name, const string gender, int age):
m_name(name), m_gender(gender), m_age(age)
{}
Person::~Person()
{}
void Person::setFather(Person *person1, Person *person2)
{
person1->m_father = person2;
}
void Person::setMother(Person *person1, Person *person2)
{
person1->m_mother = person2;
}
ostream& operator<<(ostream& os, const Person& aPerson)
{
os << "Name: " << aPerson.m_name << endl;
os << "Gender: " << aPerson.m_gender << endl;
os << "Age: " << aPerson.m_age << endl;
os << endl << "Father: " << aPerson.m_father->m_name << endl;
os << "Mother: " << aPerson.m_mother->m_name << endl;
return os;
}
int main()
{
Person newPerson("Jeff", "Male", 18);
Person father("Michael", "Male", 40);
Person mother("Sarah", "Female", 38);
cout << newPerson;
cin.get();
return 0;
}[/code]

Share this post


Link to post
Share on other sites
m_father and m_mother are never created you need to create these if you intend to use them on these lines:

[code]
os << endl << "Father: " << aPerson.m_father->m_name << endl;
os << "Mother: " << aPerson.m_mother->m_name << endl;
[/code]

Otherwise when you go to use the << operator on your new class its not going to be able to access them and fail.

Share this post


Link to post
Share on other sites
Two other kind of big deals:

Your constructor doesn't initialize your pointers to zero and just leaves them dangling and point to god knows where. If your application didn't crash while printing the person, that would be pure luck.

You seem to miss the point of a member function. Just look at how you would set a father with your code

newPerson.setFather(newPerson, father);

Can you tell what is pointless and not needed there? Passing newPerson as a parameter when a member function is ALREADY operating on the object it is called for.
[code]
void Person::setFather(Person *father)
{
m_father = father;
}
[/code]

Share this post


Link to post
Share on other sites

This topic is 2087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this