Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling operators of base class... safe?

Tags:

c++

operators

Is following pattern ok/safe ? Or are there any shortcomings ? (I also use it for equality operators)

Derived& operator=(const Derived& rhs)
{
    static_cast<Base&>(*this) = rhs;
    // ... copy member variables of Derived
    return *this;
}
like image 809
smerlin Avatar asked Jan 19 '11 11:01

smerlin


2 Answers

This is fine, but it's a lot more readable IMHO to call the base-class by name:

Base::operator = (rhs);
like image 185
Moo-Juice Avatar answered Oct 09 '22 02:10

Moo-Juice


Yes, it's safe.

A different syntax to do the same thing could be:

Base::operator=( rhs );
like image 42
peoro Avatar answered Oct 09 '22 02:10

peoro