#### Archived

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

# returning an object vie a this pointer.

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

## Recommended Posts

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 on other sites
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 on other sites
i thought setting an object that is passed by reference can be assigned to a pointer.

##### Share on other sites
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 on other sites
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.