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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inheritance of a Base class operator=

Status
Not open for further replies.

joelc

Programmer
Oct 26, 2000
5
0
0
US
I have two classes, one is the base class of the other. I would like call “operator = “ from the base class in addition to the new “operator =”. I know I am missing something simple, but I don’t know what.

class CAge
{
public:
CAge();
~CAge();
Cage & operator = (CAge &catmp)
{
SetAge(catmp.GetAge());
}
void SetAge( int iAge) {m_iAge = iAge;}
int GetAge() { return iAge;}
private:
int m_iAge;
}

class CPerson : public CAge
{
CPerson();
~CPerson();
CPerson & operator = (CPerson &cptmp)
{
SetName(cptmp.GetName());
}
void SetName(CString SName) {m_SName = SName;}
CString GetName() { return SName;}
private:
Cstring SName;
}

so

main ()
{
CPerson P1, P2;
P2.SetAge(1);
P2.SetName("A Name");
P1 = P2;
int iAge = P1.GetAge();
}


I know, in CPerson, I can add
SetAge(cptmp.GetAge());

but I thought there was automatic way. Thanks!!!
 
To call the base class method, you use the name like this:
[tt]
class MyClass
{
void MyFunc(int var1);
}

void MyClass::MyFunc(int var1)
{
// Enter your function code here
}

class AnotherClass:public MyClass
{
void MyFunc(int var1);
}

void AnotherClass::MyFunc(int var1)
{
// Call the base class method
MyClass::MyFunc(var1);
// Do subclass specific code here
}
[/tt]
I've never tried it with an overloaded operator, but it should work the same way.

Ian
 
Thanks Ian for your quick responce. I have done that before, but I can seem to get the call correct with a operator function. Any ideas.. Thanks again..
 
After experimentation, it looks like you don't have to, Joel. The compiler automatically uses the base class's operator=. Here's my example that shows this:
[tt]
#include <iostream.h>

class stats
{
public:
int x;
int y;
int z;
operator =(const stats rhs);
};

stats::operator =(const stats rhs)
{
x=rhs.x;
y=rhs.y;
z=rhs.z;
}

class Mystats:public stats
{
public:
int w;
operator =(const Mystats rhs);
};

Mystats::operator =(const Mystats rhs)
{
// Base class operator= is automatically called
w=rhs.w;
}

void main()
{
Mystats mystat;
mystat.w=9;
mystat.x=5;
mystat.y=2;
mystat.z=6;

Mystats Newstat=mystat;
// Now we'll see what the new values are:
cout << Newstat.w << Newstat.x << Newstat.y << Newstat.z << endl;
}
[/tt]
 
Thank you chpicker for your response, but your code does not use the operator= function, but a predefined copy constructor which is a bit copy of the original object. If you change your line of code

Mystats Newstat=mystat;

To

Mystats Newstat;
Newstat=mystat;

You will notice that the &quot;w&quot; member is copied, but the base class members are not.

I wish I could use a bit copy of the original object, but the code I am maintaining has a pointer to a malloc object as a class member, and when you try to free a malloc twice, things crash. (It would take a long time to clean up the mallocs, so I have to continue to put band-aids in :-( )

Again, thank you for your response. This is turning into a personal development question instead of an immediate problem.
 
Yeeeaaa. I found the solution to my own problem. You have to call the base call like this. I forgot the () the first time. Thanks for the help..

Mystats::eek:perator =(const Mystats &rhs)
{
// Base class operator=
stats::eek:perator = ( rhs );

// This class Operator=
w=rhs.w;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top