Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator= can't be inherited.

Reply
Thread Tools

Operator= can't be inherited.

 
 
Nephi Immortal
Guest
Posts: n/a
 
      02-10-2011
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;
}
 
Reply With Quote
 
 
 
 
÷Ų Tiib
Guest
Posts: n/a
 
      02-10-2011
On Feb 10, 7:06*am, Nephi Immortal <(E-Mail Removed)> wrote:
> * * * * 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:perator=(T const&) will hide all the B&
B:perator=(Something) declared by base classes. C++ may not find
them because they are hidden.
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      02-10-2011
"Nephi Immortal" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-10-2011
On Feb 10, 5:06 am, Nephi Immortal <(E-Mail Removed)> wrote:
> 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.

--
James Kanze
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      02-11-2011
On Feb 10, 7:09*am, James Kanze <(E-Mail Removed)> wrote:
> On Feb 10, 5:06 am, Nephi Immortal <(E-Mail Removed)> wrote:
>
> > 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.


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+=?
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-11-2011
On Feb 11, 3:42 am, Nephi Immortal <(E-Mail Removed)> wrote:
> On Feb 10, 7:09 am, James Kanze <(E-Mail Removed)> wrote:
> > On Feb 10, 5:06 am, Nephi Immortal <(E-Mail Removed)> wrote:


> > > 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.


> 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-lit....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.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments