By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,371 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,371 IT Pros & Developers. It's quick & easy.

operator= inherited from template

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.