Accessing assignment operators in parent classes

H

Havatcha

Does anyone know the syntax for accessing assignment operators in a
Parent Class from a Child Class? If so, would be great if you could post
a brief example here. Thanks.
 
J

John Harrison

Havatcha said:
Does anyone know the syntax for accessing assignment operators in a
Parent Class from a Child Class? If so, would be great if you could post
a brief example here. Thanks.

Yes just put Parent:: before the operator call

class Parent
{
};

class Child : public Parent
{
Child& operator=(const Child& rhs)
{
Parent::eek:perator=(rhs);
...
return *this;
}
};

Alternatively you can use a cast

class Child : public Parent
{
Child& operator=(const Child& rhs)
{
*static_cast<Parent*>(this) = rhs;
...
return *this;
}
};

john
 
H

Havatcha

Yes just put Parent:: before the operator call

class Parent
{
};

class Child : public Parent
{
Child& operator=(const Child& rhs)
{
Parent::eek:perator=(rhs);
...
return *this;
}
};

Alternatively you can use a cast

class Child : public Parent
{
Child& operator=(const Child& rhs)
{
*static_cast<Parent*>(this) = rhs;
...
return *this;
}
};

john


Lovely, thanks very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top