Jump to content
  • Advertisement
Sign in to follow this  
sdroadie

Returning a data member of a pointer

This topic is 2448 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?

#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;
}

Share this post


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


os << endl << "Father: " << aPerson.m_father->m_name << endl;
os << "Mother: " << aPerson.m_mother->m_name << endl;


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.

void Person::setFather(Person *father)
{
m_father = father;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!