Inherited operator = not being seen

Inherited operator = not being seen

Question:
I have a templated class A that defines two operator =, and a templated class B : public class A which should inherit the two virtual a::operator = I define, but when I instantiate an object B b, it won’t see the operator = .

Answer:
You are not going to like this, but the operator = is a specialmember function that is not inherited, in order to prevent slicing of objects.

Consider:

struct Base{	const Base& operator=(const Base& b) {i = b.i;};private:	int i;};struct Derived : public Base{private:	int i;};
If the assignment were to be inherited, I could then say:Derived d1,d2;d1 = d2;and only the Base subobject of d1 would get copied, not the Derived part.

To solve this problem, the compiler generates an assignment operator for classesthat don’t define them (when it can ? but that’s another story); the rules forthat is member-wise, copy starting with the base classes. So, the compiler-generated assignment operator would look something like this:

inline Derived& Derived::operator(const Derived& d){	Base::operator=(d);	i = d.i;	return *this;}
Note that the generated copy assignment operator is inline.

This interesting property of assignment operators also makes it impossible forthe assignment to be virtual, because it’s not even visible to the clients ofthe derived class.

So that was the gist of your problem; now, let’s try to work out a solution.

Let’s rewrite our example classes now and define operator = for the derivedclass, too.

struct Base{	const Base& operator=(const Base& b) {assign(b);return *this;};protected:	void assign (const Base& b) { i = b.i;}private:	int i;};struct Derived : public Base{	const Derived&  operator = (const Derived& d) {assign(d);return *this;};protected:	void assign(const Base& b) 	{		// call base classes assign		Base::assign(b);		try		{			const Derived &d = dynamic_cast(b);			i = d.i;		}		catch (const bad_cast&)		{			// do appropriate error handling		}	}private:	int i;};
So now if we have
Derived d1,d2;
and we say
d1 = d2; // this will call Derived::operator =() which calls assign and // propagates all the way to the base class and then copies itself.
This has the added advantage of making the assignment operator in thebase class behave polymorphically. For example, if we had
Base * b1 = new Derived;Base * b2 = new Derived;
*b1 = *b2; // would still get the entire object assigned, as opposed to justthe base class subobject.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as