473,498 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator= inherited from template

Hi all!

I'm getting some for me "unexpected behaviour" from g++ 4.1.2 when
compiling this simple test program:

#include <cstdlib>
#include <cstdio>
using namespace std;

template<typename t>
class MyTemplate
{
public:
inline void operator=(const t& val)
{
printf("%d\n", (int)val);
}
};

class MyClass : public MyTemplate<short>
{};

int main()
{
MyClass abc;
abc=5;
//((MyTemplate<short>&)abc)=5;
return EXIT_SUCCESS;
}

It complains that operator= is not defined for MyClass -- however, to my
understanding of C++, it should be inhertied like a simple function?

The program works as expected when I either replace abc=5; with the line
commented out below or when I replace operator= by a simple function like
inline void foo(const t& val)
and
abc.foo(5);

Are operators handled differently from ordinary functions when
inheriting or maybe when inheriting from templates?

Many thanks in advance,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
May 26 '07 #1
7 1674
Daniel Kraft wrote:
Hi all!

I'm getting some for me "unexpected behaviour" from g++ 4.1.2 when
compiling this simple test program:

#include <cstdlib>
#include <cstdio>
using namespace std;

template<typename t>
class MyTemplate
{
public:
inline void operator=(const t& val)
{
printf("%d\n", (int)val);
}
};

class MyClass : public MyTemplate<short>
{};

int main()
{
MyClass abc;
abc=5;
//((MyTemplate<short>&)abc)=5;
return EXIT_SUCCESS;
}

It complains that operator= is not defined for MyClass -- however, to my
understanding of C++, it should be inhertied like a simple function?

The program works as expected when I either replace abc=5; with the line
commented out below or when I replace operator= by a simple function like
inline void foo(const t& val)
and
abc.foo(5);

Are operators handled differently from ordinary functions when
inheriting or maybe when inheriting from templates?
The problem is that operator= is defined automatically for each class.
So the auto-generated operator= for MyClass is hiding the operator= in
MyTemplate.

You would see exactly the same behaviour with foo

template<typename t>
class MyTemplate
{
public:
void foo(const t& val);
};

class MyClass : public MyTemplate<short>
{
void foo(const MyClass&);
};

int main()
{
MyClass abc;
abc.foo(5); // cannot convert 5 to MyClass
}

You might be able to get round this with a using declaration

class MyClass : public MyTemplate<short>
{
public:
using MyTemplate<short>::operator=(const short&);
};

Haven't tested that though.
Many thanks in advance,
Daniel
May 26 '07 #2
Daniel Kraft wrote:
It complains that operator= is not defined for MyClass -- however, to my
understanding of C++, it should be inhertied like a simple function?
No. operator= is not inherited.
Are operators handled differently from ordinary functions when
inheriting or maybe when inheriting from templates?
Only operator=.

May 26 '07 #3
>It complains that operator= is not defined for MyClass -- however, to my
>understanding of C++, it should be inhertied like a simple function?
Ok, thank you so far! So I will probably work around by doing something
like:

inline void operator=(const t& val)
{
return SuperClass::operator=(val);
}

which should work without problems; while of course inheriting operator=
would be the "best" solution for my concrete problem, that does not hurt
much, anyway.

Yours,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
May 26 '07 #4
Rolf Magnus wrote:
Daniel Kraft wrote:
>It complains that operator= is not defined for MyClass -- however, to my
understanding of C++, it should be inhertied like a simple function?

No. operator= is not inherited.
>Are operators handled differently from ordinary functions when
inheriting or maybe when inheriting from templates?

Only operator=.
If operator= is not inherited then why does this work?
class A
{
public:
void operator=(int);
};

class B : public A
{
public:
using A::operator=;
};

int main()
{
B b;
b = 1;
}
I think it is more accurate to say that operator= is inherited but it is
usually hidden by the derived class' operator=.

john
May 26 '07 #5
On 26 Maj, 09:54, Rolf Magnus <ramag...@t-online.dewrote:
Daniel Kraft wrote:
It complains that operator= is not defined for MyClass -- however, to my
understanding of C++, it should be inhertied like a simple function?

No. operator= is not inherited.
Are operators handled differently from ordinary functions when
inheriting or maybe when inheriting from templates?

Only operator=.
operator= is inherited just like any other operator or function. What
happens is name lookup: the automatically generated operator= is
found, and this prevents the base-class operator= from having any
effect.

Some compilers might have problems with a using declaration for
operator=, but that is a problem with the implementation, not because
of special rules for operator=.

/Peter

May 26 '07 #6
John Harrison wrote:
Rolf Magnus wrote:
>Daniel Kraft wrote:
>>It complains that operator= is not defined for MyClass -- however, to my
understanding of C++, it should be inhertied like a simple function?

No. operator= is not inherited.
>>Are operators handled differently from ordinary functions when
inheriting or maybe when inheriting from templates?

Only operator=.

If operator= is not inherited then why does this work?
class A
{
public:
void operator=(int);
};

class B : public A
{
public:
using A::operator=;
};

int main()
{
B b;
b = 1;
}
Because you explicitly introduce it to class B.
I think it is more accurate to say that operator= is inherited but it is
usually hidden by the derived class' operator=.
Depends a bit on how you define "inherited", but I think your wording is
indeed more accurate.

May 26 '07 #7
On May 26, 1:24 pm, Daniel Kraft <d...@domob.euwrote:
understanding of C++, it should be inhertied like a simple function?
operator= is a very special member function in two ways:
1.It can not be defined as a binary static/none-member operator
wheras other operators (say operator+(left,right)) can.
2.It is not inherited.
the general inheritance behavior of this operator resembles the copy-
constructor:
1.if A class 'C1' hides its operator=(C1) then inherited classes and
classes that have a data member of 'C1' type can not automatically
generate an operator= .
2.it is overloadable with other parameters but not inheritable;the
inherited class must define its own operator= -in case the operand to
that of base is not of the self type- which calls the base:

class base{
public:
base& operator=(int);//not inherited
//private://if uncommented derived will not be assignable
base& operator=(base&);
};

class derived:public base{};

derived d1, d2;

d1=d2;/*as long as base::operator=(base&) is public it is ok,otherwise
error*/

d1=1;//error: base& base::operator=(int) not inheritable.
The program works as expected when I either replace abc=5; with the line
just as expected.
Are operators handled differently from ordinary functions when
not the others.
inheriting or maybe when inheriting from templates?
***No.***

May 26 '07 #8

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

Similar topics

6
13980
by: Juha Tenhosaari | last post by:
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;
2
1286
by: jmborr | last post by:
Hi all, For some reason, my derived class does not inherit the operator '=' of my base class, which is a class template, when I compile with g++ //=========== base class template<class T> class...
15
2136
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
5
1947
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit...
3
1680
by: Drew McCormack | last post by:
Does operator() get inherited? I would have thought so, but I can't get the following to work: I have a base class that takes its derived class as a template template parameter (the ...
7
2307
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
3
2927
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
11
1950
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
3
1747
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7200
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...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
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...
0
5460
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,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3090
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
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 ...

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.