Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
.................................................................................
Posted
Updated 29-Aug-11 19:21pm
v4

In printDad function, replace
Member 8186414 wrote:
cout << p.getAge(p.getDad()) << endl;

with
C++
cout << p.getDad()->getAge() << endl;
 
Share this answer
 
Comments
André Kraak 24-Aug-11 17:01pm    
+5 You beat me to it.
CPallini 25-Aug-11 2:55am    
Thank you.
fjdiewornncalwe 24-Aug-11 19:57pm    
Absolutely.
CPallini 25-Aug-11 2:56am    
Thanks.
TheyCallMeMrJames 25-Aug-11 9:05am    
+5
Not strictly related to the question but ...
Be careful about
void printDad(Person p) 

It actually doesn't refer to the person you mean, but to a copy of it.
That -because of how C++ default copy constructor works- has the same members values (hence point to the same mom and dad) and hence incidentally will work the same, but it is actually a "temporary brother".
Pass a pointer or a reference, not a value.

Also, learn to use const for member function that don't modify the object, and const& for parameters. You'll avoid a lot useless copy.
 
Share this answer
 
Comments
CPallini 25-Aug-11 4:03am    
Good note.
void printDad(Person& p) // cause i cant see a copy constructor
{
  if(p.getDad())
    cout << p.getDad()->getAge() << endl;
  else
    cout << "orphan" << endl;
}

or implement a copy constructor:
class Person
{
  Person* mom,
  *       dad;
  int     age;
public:
  Person(){ mom=dad=0; age=0; }
  Person(Person& init){ mom=init.mom; dad=init.dad; age=init.age; }
   // the others
};

Good luck.
 
Share this answer
 
Comments
Stefan_Lang 25-Aug-11 4:47am    
Good point about the missing copy constructor, but it isn't actually required since the compiler will always create one if you don't specify one yourself. The compiler-created copy constructor will do a shallow copy which in this case is sufficient.

The only way to prevent the compiler form creating a copy constructor is to use the '=delete' syntax which only works for the new standard C++0x, e. g.:
class Person {
Person(const Person&)=delete;//'defines' the copy constructor as non-existant
//...
};
mbue 25-Aug-11 10:29am    
To deal with pointers is a difficult thing (especially in linked objects). without explicit copy contructor the pointers will have no owners. the default copy constructor cannot distinguish between owners (its a simple memcopy). so the copy constructor can duplicate the pointers if neccessary. default copy constructor cannot do that.
Regards.
Stefan_Lang 25-Aug-11 10:50am    
>> 'its a simple memcopy'
Yes I know, that's what I meant by 'shallow copy'.

Fortunately in this example main() creates all the Person objects, so it just so happens that everything works out!

But I agree that it's generally a good idea to provide a copy constructor of your own, especially when your class contains pointer members. There's just too much that can go wrong!
mbue 25-Aug-11 12:26pm    
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900