473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Inheriting overloaded operators

I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain
overloaded operators don't seem to come with the inheritance. For example:

// TestA.h ---------------------------------------

#include <iostream>

enum Aval
{
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE
};

class A
{
private:
Aval value;

public:
A()
{
value = Aval(0);
std::cout << "A() constructor" << std::endl;
}

A( Aval val ) : value(val)
{
std::cout << "A(val) constructor" << std::endl;
}
virtual ~A();

virtual A & operator=( const A & otherError );
virtual A & operator=( const Aval & otherValue );

virtual Aval GetValue() { return value; }
};
// TestA.cpp --------------------------------------

#include "TestA.h"
#include <iostream>

A::~A()
{
std::cout << "A destructed" << std::endl;
}

A & A::operator=( const A & otherError )
{
// Check for self-assignment
if (this == &otherError)
{
return *this;
}

value = otherError.value;
return *this;
}
A & A::operator=( const Aval & otherValue )
{
value = otherValue;
return *this;
}

// Test B.h --------------------------------------

#include "TestA.h"
#include <iostream>
class B : public A
{
public:
B()
{
std::cout << "B() constructor" << std::endl;
}

virtual ~B()
{
std::cout << "B destructed" << std::endl;
}

};
// main.cpp --------------------------------------

int main( int argc, char* argv[] )
{
A testA;
B testB;

testA = FIRST_VALUE;
testB = SECOND_VALUE;

return 0;
}
When compiling this in VC++ 6.0, I get the error for the "testB = SECOND_VALUE" line of code:

error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'enum Aval' (or there is no
acceptable conversion).

The "testA = FIRST_VALUE" seems to be fine. Is this a VC++ 6.0 quirk or am I doing something wrong? Why won't B
inherit the overloaded operator?

Thanks.
Jul 19 '05 #1
5 6857
"Andy Jarrell" <an***@dacaudio.com> wrote...
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example:
// TestA.h ---------------------------------------

#include <iostream>

enum Aval
{
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE
};

class A
{
private:
Aval value;

public:
A()
{
value = Aval(0);
std::cout << "A() constructor" << std::endl;
}

A( Aval val ) : value(val)
{
std::cout << "A(val) constructor" << std::endl;
}
virtual ~A();

virtual A & operator=( const A & otherError );
virtual A & operator=( const Aval & otherValue );

virtual Aval GetValue() { return value; }
};
// TestA.cpp --------------------------------------

#include "TestA.h"
#include <iostream>

A::~A()
{
std::cout << "A destructed" << std::endl;
}

A & A::operator=( const A & otherError )
{
// Check for self-assignment
if (this == &otherError)
{
return *this;
}

value = otherError.value;
return *this;
}
A & A::operator=( const Aval & otherValue )
{
value = otherValue;
return *this;
}

// Test B.h --------------------------------------

#include "TestA.h"
#include <iostream>
class B : public A
{
public:
B()
{
std::cout << "B() constructor" << std::endl;
}

virtual ~B()
{
std::cout << "B destructed" << std::endl;
}

};
// main.cpp --------------------------------------

int main( int argc, char* argv[] )
{
A testA;
B testB;

testA = FIRST_VALUE;
testB = SECOND_VALUE;

return 0;
}
When compiling this in VC++ 6.0, I get the error for the "testB = SECOND_VALUE" line of code:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'enum Aval' (or there is no acceptable conversion).

The "testA = FIRST_VALUE" seems to be fine. Is this a VC++ 6.0 quirk or am I doing something wrong? Why won't B inherit the overloaded operator?


Because the B::operator=(const B&), which is _implicit_ and provided
by the compiler for you, _hides_ all others.

Victor
Jul 19 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<YTNsb.193609$e01.709899@attbi_s02>...
"Andy Jarrell" <an***@dacaudio.com> wrote...
I'm trying to inherit from a specific class that has an overloaded

operator. The problem I'm getting is that certain
overloaded operators don't seem to come with the inheritance. For

example:

// TestA.h ---------------------------------------

#include <iostream>

enum Aval
{
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE
};

class A
{
private:
Aval value;

public:
A()
{
value = Aval(0);
std::cout << "A() constructor" << std::endl;
}

A( Aval val ) : value(val)
{
std::cout << "A(val) constructor" << std::endl;
}
virtual ~A();

virtual A & operator=( const A & otherError );
virtual A & operator=( const Aval & otherValue );

virtual Aval GetValue() { return value; }
};
// TestA.cpp --------------------------------------

#include "TestA.h"
#include <iostream>

A::~A()
{
std::cout << "A destructed" << std::endl;
}

A & A::operator=( const A & otherError )
{
// Check for self-assignment
if (this == &otherError)
{
return *this;
}

value = otherError.value;
return *this;
}
A & A::operator=( const Aval & otherValue )
{
value = otherValue;
return *this;
}

// Test B.h --------------------------------------

#include "TestA.h"
#include <iostream>
class B : public A
{
public:
B()
{
std::cout << "B() constructor" << std::endl;
}

virtual ~B()
{
std::cout << "B destructed" << std::endl;
}

};
// main.cpp --------------------------------------

int main( int argc, char* argv[] )
{
A testA;
B testB;

testA = FIRST_VALUE;
testB = SECOND_VALUE;

return 0;
}
When compiling this in VC++ 6.0, I get the error for the "testB =

SECOND_VALUE" line of code:

error C2679: binary '=' : no operator defined which takes a right-hand

operand of type 'enum Aval' (or there is no
acceptable conversion).

The "testA = FIRST_VALUE" seems to be fine. Is this a VC++ 6.0 quirk or

am I doing something wrong? Why won't B
inherit the overloaded operator?


Because the B::operator=(const B&), which is _implicit_ and provided
by the compiler for you, _hides_ all others.

Victor


So for all implicit operators I must specifically overload them. I.e.,

class B : public A
{
public:
virtual B & operator=( const B & otherError );
virtual B & operator=( const Aval & otherValue );
// yadda, yadda, . . .
}

And them implement them, calling the base class operator if I wish.

But, the following compiles without a hitch:

A testA1, testA2;
B testB1, testB2;

testA2 = testA1;
testB1 = testB2;

Is it that the implicit "=" operator is called for testB1, whereas the overloaded operator is called for testA1. I
think I just answered my own question.

Does the same hold true for implicit things like the copy constructor? How do I inherit operators from the base class?

Thanks for your insight.

Andy
Jul 19 '05 #3
"Andy Jarrell" <an***@dacaudio.com> wrote...
[...]
But, the following compiles without a hitch:

A testA1, testA2;
B testB1, testB2;

testA2 = testA1;
testB1 = testB2;

Is it that the implicit "=" operator is called for testB1, whereas the overloaded operator is called for testA1. I think I just answered my own question.
You did.

Does the same hold true for implicit things like the copy constructor?

How do I inherit operators from the base class?

Try delcaring them as used:

class A {
...
};

class B : public A {
...
using A::operator=; // adds this name to this scope
};

Victor

Jul 19 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:ClRsb.195128$e01.712657@attbi_s02...
"Andy Jarrell" <an***@dacaudio.com> wrote...
[...]
But, the following compiles without a hitch:

A testA1, testA2;
B testB1, testB2;

testA2 = testA1;
testB1 = testB2;

Is it that the implicit "=" operator is called for testB1, whereas the

overloaded operator is called for testA1. I
think I just answered my own question.


You did.

Does the same hold true for implicit things like the copy constructor?

How do I inherit operators from the base class?

Try delcaring them as used:

class A {
...
};

class B : public A {
...
using A::operator=; // adds this name to this scope
};

Victor

To answer my own question (yet again), the assignment operator is not inherited for understandable reasons as most
derived classes have additional data members for which the base class assignment operator would not work. For those
instances where the base class assignment operator I tried your suggestion:
using A::operator=;
in the class definition. The other method was to just rewrite the operator and axplicitly call the base operator by
doing something like:
virtual B & operator=( const B& other )
{
A::operator=( otherValue );
return *this;
}

This works for the basic class assignment. Thanks for the insight. I also came across another post you made (God bless
Google Groups) directing some users to items 15/16 in Scott Meyers Effective C++. I reread my copy and it helped clear
things up.

I've narrowed the problem I was experiencing down to this:

class A
{
private:
int data;
public:
virtual A & operator=( const int value );
}

class B : public A
{
public:
virtual B & operator=( const int value );
}

The problem is that I'm now getting the error:
'B::=' : overriding virtual function differs from 'A::=' only by return type or calling convention

I understand that differing return types are not permissible ways of overloading. But, it seems to me that this is the
basic function overloading. I mean, the function could just as much have been:

class A
{
virtual A & foo(int a);
}

class B : public A
{
virtual B & foo(int b);
}

The above example would work. But the problem was that I was declaring the operators virtual, which probably confused
the heck out of the compiler. Anyway, by changing them to:

class A
{
private:
int data;
public:
A & operator=( const int value );
}

class B : public A
{
public:
B & operator=( const int value )
{
A::operator=( otherError );
return *this;
}
}

It worked. Working of Scott Meyers syntax I could also have done:
static_cast<A&>(*this) = value;
return *this;

Thought I'd post this for posterity.

Andy
Jul 19 '05 #5
"Andy Jarrell" <an***@dacaudio.com> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:ClRsb.195128$e01.712657@attbi_s02...
"Andy Jarrell" <an***@dacaudio.com> wrote...
[...]
But, the following compiles without a hitch:

A testA1, testA2;
B testB1, testB2;

testA2 = testA1;
testB1 = testB2;

Is it that the implicit "=" operator is called for testB1, whereas the

overloaded operator is called for testA1. I
think I just answered my own question.


You did.

Does the same hold true for implicit things like the copy constructor?

How do I inherit operators from the base class?

Try delcaring them as used:

class A {
...
};

class B : public A {
...
using A::operator=; // adds this name to this scope
};

Victor

To answer my own question (yet again), the assignment operator is not

inherited for understandable reasons as most derived classes have additional data members for which the base class assignment operator would not work. For those instances where the base class assignment operator I tried your suggestion: using A::operator=;
in the class definition. The other method was to just rewrite the operator and axplicitly call the base operator by doing something like:
virtual B & operator=( const B& other )
{
A::operator=( otherValue );
return *this;
}

This works for the basic class assignment. Thanks for the insight. I also came across another post you made (God bless Google Groups) directing some users to items 15/16 in Scott Meyers Effective C++. I reread my copy and it helped clear things up.

I've narrowed the problem I was experiencing down to this:

class A
{
private:
int data;
public:
virtual A & operator=( const int value );
}

class B : public A
{
public:
virtual B & operator=( const int value );
}

The problem is that I'm now getting the error:
'B::=' : overriding virtual function differs from 'A::=' only by return type or calling convention

There should be no such warning because the two functions have
_covariant_ return types. It is expressly permitted by the Standard.
Your compiler is likely out of date in that regard.

I understand that differing return types are not permissible ways of overloading. But, it seems to me that this is the basic function overloading. I mean, the function could just as much have been:
class A
{
virtual A & foo(int a);
}

class B : public A
{
virtual B & foo(int b);
}

The above example would work. But the problem was that I was declaring the operators virtual, which probably confused the heck out of the compiler.
No. There is no _overloading_ based on return value types. It is
a common confusion point, so don't worry.

_Overloading_ only concerns _argument_ number,types, and order.
Anyway, by changing them to:

class A
{
private:
int data;
public:
A & operator=( const int value );
}

class B : public A
{
public:
B & operator=( const int value )
{
A::operator=( otherError );
return *this;
}
}

It worked. Working of Scott Meyers syntax I could also have done:
static_cast<A&>(*this) = value;
return *this;

Thought I'd post this for posterity.


Good idea.

Victor
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pete Wilson | last post by:
I'm at the midpoint of Intro to C++. Overloaded operators are opaque to me; I don't get it at all. Where can I find a web tutorial that is plain and complete on the subject? Thanks!
3
by: N4M | last post by:
Dear, I have problems with overloaded operators ++() and --(). MSVC++ 6.0 compiler gives errors, one is shown as below: " c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++' :...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
3
by: ES Kim | last post by:
iterator classes provide overloaded operators like this in general: template <typename T> class Iterator { Iterator operator++(int); // postfix ++ T& operator*(); }; Iterator<int> i;
3
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
4
by: Daniel Kraft | last post by:
Hi all, I'd like to know your opinion on when you think overloaded operators should/could be used instead of "ordinary methods". Of course, they are essential for generic programming and there...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.