Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading Operator =

Status
Not open for further replies.

nyjil

Programmer
Jul 14, 2000
32
US
I'm just about ready to pull my hair out of my head. This should be a simple concept, but it is proving to confound me in every which manner. All I want to do is to assign the value of one object to the values of another object, i.e.

CDog Fido;
CDog Spot;
Fido = Spot;

This is simple enough, and works unless you derive your object from something else. In this case I derived my object from CObject, so it's Declaration looks something like this:

Class CDog : public CObject
{
DECLARE_SERIAL (CDog)
public:
constructor
destructor
copy constructor
yada, yada, yada
private:
int age;
int weight;
CString name;
};

Since I have derived the CDog object from CObject, the compiler doesn't recognize the '=' operator function and I have to make my own. No problem, I'll just overload it and make my own. Problem is, I'm having big troubles doing it. What I'm asking is for someone to help me with some code on overloading the '=' operator and maybe help me out with the copy constructor as well. I've traced my problems to those two areas. Any help on this matter would be HUGELY appriciated.

Thanks,
Nyjil
 
the operator overloaded would look like

void operator = (CDog d)
{
member_var1 = d.member_var1;
member_var2 = d.member_var2;
etc...
}


The copy constructor could call this by the line

*this = dogToCopy;

Matt
 
Thank you, thank you, thank you. I knew I was making it just too complicated. It was indeed very simple, as you pointed out for me.

Again, many thanks

Nyjil
:-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top