473,703 Members | 3,336 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.valu e;
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 6883
"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.valu e;
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=(co nst B&), which is _implicit_ and provided
by the compiler for you, _hides_ all others.

Victor
Jul 19 '05 #2

"Victor Bazarov" <v.********@com Acast.net> wrote in message news:<YTNsb.193 609$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.valu e;
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=(co nst 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.********@com Acast.net> wrote in message news:ClRsb.1951 28$e01.712657@a ttbi_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.********@com Acast.net> wrote in message news:ClRsb.1951 28$e01.712657@a ttbi_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
314
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
1704
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::++' : overriding virtual function differs from 'CGraphNodeIterI::++' only by return type or calling convention c:\data\c++\mygraphs\graph.h(141) : see declaration of 'CGraphNodeIterI' "
4
1613
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 Edition that I have posted to comp.sources.d). How do we decide which is more appropriate? Why are the overloaded "<<" and ">>" operators always friends? Also, what is an appropriate application for the overloaded function call operator?
1
10002
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 A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
13
2000
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) depending on the operator: class Derived : public Base { };
3
1707
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
1744
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 default arguments. In my class, the operator = works for another vector (just copying the elements), and for a double: in this cases each element of the vector will be equal to the double. Example:
4
1400
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 are some nice "hacks" for expression templates, like Boost's Spirit library. But personally, I tend to use them also for "methods" of objects, which to something "conceptually similar" to what the overloaded operator does usually.
0
8662
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9243
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7853
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.