operator inheritance problem

J

Juha Tenhosaari

Hello,

I wonder if anyone could tell me why the operator= is not inherited into the
derived class in the following code:

class A
{
public:
void operator=(int i){a=i;}
int a;
}

class B : public A
{
public:
int b;
}

void main(void)
{
B bb;
bb=3; // trying to use the overloaded operator
cout << bb.a;
}

When I try to compile this, I get the error message "could not find match
for B::eek:perator=(int)". If I replace the overloaded operator by an ordinary
function (i.e. operator=(int) => set(int)), it works fine.

What am I doing wrong?


Juha
 
J

John Carson

Juha Tenhosaari said:
Hello,

I wonder if anyone could tell me why the operator= is not inherited
into the derived class in the following code:

class A
{
public:
void operator=(int i){a=i;}
int a;
}

class B : public A
{
public:
int b;
}

void main(void)
{
B bb;
bb=3; // trying to use the overloaded operator
cout << bb.a;
}

When I try to compile this, I get the error message "could not find
match for B::eek:perator=(int)". If I replace the overloaded operator by
an ordinary function (i.e. operator=(int) => set(int)), it works fine.

What am I doing wrong?


Juha


Simple. Assignment operators are not inherited --- just like constructors
are not inherited.
 
T

Tim Threlfall

13.5.3 of the standard:

Because a copy assignment operator operator= is implicitly declared for
a class if not declared by the user (12.8), a base class assignment
operator is always hidden by the copy assignment operator of the
derived class.
 
G

Guest

Simple. Assignment operators are not inherited --- just like constructors
are not inherited.
They are inherited but hiddem , see response from Tim.

Adding using declaration to class B :

using A::eek:perator =(int);

will bring it back to the public/protected/private view.
 
J

John Carson

They are inherited but hiddem , see response from Tim.

Adding using declaration to class B :

using A::eek:perator =(int);

will bring it back to the public/protected/private view.



I think this satisfies my definition of "not inherited" --- and apparently
Stroustrup's too (see TC++PL, p. 307). Nevertheless, the technique for
making it accessible to the derived class is worth pointing out. However,
the using declaration should not specify the operator's argument. It should
be:

using A::eek:perator = ;

Overload resolution will then occur in the usual way.
 
J

Juha Tenhosaari

John said:
the using declaration should not specify the operator's argument. It should
be:

using A::eek:perator = ;

Thanks for the answers.

I modified the derived class this way:

class B : public A
{
using A::eek:perator=;
public:
int b;
}


....but my compiler gave me error message "Identifier expected". I wonder
why.

This, however, did work:

class B : public A
{
public:
void operator=(int i){A::eek:perator=(i);}
int b;
}


Juha
 
J

John Carson

Juha Tenhosaari said:
Thanks for the answers.

I modified the derived class this way:

class B : public A
{
using A::eek:perator=;
public:
int b;
}


...but my compiler gave me error message "Identifier expected". I
wonder why.


You don't say the line at which this error message occurs. I believe that by
putting the using declaration at the start of the class, you have implicitly
made the operator private. Try moving it after public:
Note that compiler implementation of using declarations are very buggy, so I
would not be surprised if it didn't work.

After moving the using declaration to after public:, the code compiles find
using Dinkum Exam.

http://www.dinkumware.com/exam/dinkumExam.aspx
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top