operator= inherited from template

J

John Harrison

Daniel said:
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>::eek:perator=(const short&);
};

Haven't tested that though.
 
R

Rolf Magnus

Daniel said:
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=.
 
J

John Harrison

Rolf said:
No. operator= is not inherited.


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::eek:perator=;
};

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
 
R

Rolf Magnus

John said:
If operator= is not inherited then why does this work?


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

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

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.
 
P

peter koch

No. operator= is not inherited.


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
 
D

Daniel Kraft

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
 
D

Daniel Kraft

It complains that operator= is not defined for MyClass -- however, to my
Ok, thank you so far! So I will probably work around by doing something
like:

inline void operator=(const t& val)
{
return SuperClass::eek:perator=(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
 
T

terminator

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::eek:perator=(base&) is public it is ok,otherwise
error*/

d1=1;//error: base& base::eek:perator=(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.***
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top