Operator= can't be inherited.

N

Nephi Immortal

Please explain why operator= cannot be inherited into class B from
class A. Is it because the left reference before operator= is type A,
but not type B? Then, do I have to define a copy of operator= and
replace left reference to type B in class B body?
It is annoying to redefine operator= every time I create several new
classes that are derived from class A.

class A {
public:
A() : m_data( 0 ) {}
~A() {}

A &operator=( int byte ) {
m_data = byte;
return *this;
}

int m_data;
};

class B : public A {
public:
B() : A() {}
~B() {}

};

int main () {
A a;
a = 5; // ok

B b;
b = 7; // error

return 0;
}
 
Ö

Öö Tiib

        Please explain why operator= cannot be inherited into class B from
class A.  Is it because the left reference before operator= is type A,
but not type B?  Then, do I have to define a copy of operator= and
replace left reference to type B in class B body?
        It is annoying to redefine operator= every time I create several new
classes that are derived from class A.

That is because operator=() is special. When you do not declare copy-
assignment for a class then compiler declares one for you. Therefore
there will be always one operator=() present (copy-assignment) for a
class.

That always present T& T::eek:perator=(T const&) will hide all the B&
B::eek:perator=(Something) declared by base classes. C++ may not find
them because they are hidden.
 
P

Paul

Nephi Immortal said:
Please explain why operator= cannot be inherited into class B from
class A.
Inheritance works the other way.
A doesn't inherit from B, B inherits from A.
Is it because the left reference before operator= is type A,
but not type B? Then, do I have to define a copy of operator= and
replace left reference to type B in class B body?
It is annoying to redefine operator= every time I create several new
classes that are derived from class A.

You also need to overload these operators to allow for:
bObject1 = bObject2;
if you want this behaviour.
With your code your "right hand side" operand can only be an int type.

It looks like you'd be better off with using/setter methods.
If you do this you should also consider making these virtual so you have the
following behaviour:
A* a_ptr;

if( whatever) a_ptr=new(B);
else a_ptr=new(C);

int x = a_ptr->get_mData();
/*If you made get_mData virtual in A, and implemented it in each derived
dclass,then the correct function will will be called whether it's a B or C
type*/

The above demonstrates the importance of understanding the direction of
inheritance.
The same can be done with operator=() functions but complications arise as
the return type differs with inheritance. You need to write an operator=
function for each type, if you want that kind of behaviour. There is no easy
solution.


HTH
 
J

James Kanze

Please explain why operator= cannot be inherited into class
B from class A.

For the same reason other functions aren't be inherited if you
define a function with the same name in the derived class.
Is it because the left reference before operator= is type A,
but not type B? Then, do I have to define a copy of operator=
and replace left reference to type B in class B body?

It's more because every class has an operator=; if you don't
define one, the compiler will do it for you.
It is annoying to redefine operator= every time I create
several new classes that are derived from class A.

I'm not sure what you're trying to accomplish, but assignment
and inheritance don't generally work well together to begin
with.
 
N

Nephi Immortal

For the same reason other functions aren't be inherited if you
define a function with the same name in the derived class.


It's more because every class has an operator=; if you don't
define one, the compiler will do it for you.


I'm not sure what you're trying to accomplish, but assignment
and inheritance don't generally work well together to begin
with.

Ok. I understand, but I want to ask why operator= can’t be
inherited.

For example, Object &operator=( const Object & ) cannot be inherited
and it is provided to all classes by C++ Compiler unless you define
your own operator= explicitly.

Now, I am asking. What about Object &operator=( const int )? It is
not provided by C++ Compiler and it should be inherited.

Also, and what about operator int() as cast operator? Can it be
inherited?

And what about other operators such as operator[], operator++, and
operator+=?
 
J

James Kanze

Ok. I understand, but I want to ask why operator= can’t be
inherited.

For the same reason no function is inherited if you declare
a function of the same name in the derived class. operator=
follows exactly the same rules as every other function.

Consider:

struct Base
{
void f(int);
void f(std::string const&);
};

struct Derived : Base
{
void f(std::string const&);
};

int
main()
{
Derived d;
d.f(1);
return 0;
}

Doesn't compile either. (Worse, d.f(0) compiles, but crashes at
runtime, because it tries to convert the null pointer constant
into a string, in order to call d.f(std::string const&).)

See http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9..
For example, Object &operator=( const Object & ) cannot be inherited
and it is provided to all classes by C++ Compiler unless you define
your own operator= explicitly.
Now, I am asking. What about Object &operator=( const int )? It is
not provided by C++ Compiler and it should be inherited.

Declaring any function with a given names in a derived class
blocks the inheritance of all functions of that name. This is
a basic principle of C++ name lookup: once the compiler finds
a name, it stops.
Also, and what about operator int() as cast operator? Can it be
inherited?
And what about other operators such as operator[], operator++, and
operator+=?

Operators follow exactly the same rules as any other function.
With no exceptions.
 

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,902
Latest member
Elena68X5

Latest Threads

Top