Archived

This topic is now archived and is closed to further replies.

GekkoCube

returning an object vie a this pointer.

Recommended Posts

GekkoCube    116
lets say i have a class called Employee. and another class called Person. now lets say that the Employee class looks something like this, roughly, for simplicity''s sake.
class Employee {
 public:
   void SetPersonInfo( Person &person)
   {
      m_person = person;
   }

 private:
   Person *m_person;
};
this doesnt work for some odd reason. i believe i need to explicity define a copy constructor or the = operator. the error i receive is
: error C2679: binary ''='' : no operator defined which takes a right-hand operand of type ''class CGDPGraph'' (or there is no acceptable conversion)
im just confused when i need to define a copy constructor and when i need to define an = operator. could someone shed some light please?

Share this post


Link to post
Share on other sites
tortoise    122
You''re trying to assign an actual Person object (passed in by reference) to a Person pointer. It is confusing that & is both used to indicate pass by reference and the address of an element.

Depending on the Person class, I''d probably just make m_person an object, not a pointer. It''s safer. In that case also make it SetPersonInfo(const Person &person).

Share this post


Link to post
Share on other sites
tortoise    122
Sure it can, but a pointer stores an address, not an object. You need to give that pointer the address of person.

m_person = &person;

I didn''t think you wanted that because it strikes me as a bit odd (but I obviously have little context to go on).

Share this post


Link to post
Share on other sites
GekkoCube    116
oh gee, i meant m_person = &person.

i am ultimately going to use m_person within th Employee class...something liek this...


person->GetName();
person->GetAge();


you get the picture.

Share this post


Link to post
Share on other sites