What the assignment operator from other type(with template) doesn't work?


P

PengYu.UT

The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.

Thanks,
Peng


/* main.cc */
template <typename U>
class A{
public:
A() { }
A &operator=(A &that) {
_internal = that._internal;
}
template <typename V>
A &operator=(V &that) {
_internal = that.get_internal();
}
private:
U _internal;
};

int main(int argc, char *argv[])
{
A<int> a;
A<double> b = a;//error
}

/* error message */
g++-3.3 -g -I/usr/local/include/clapack/ -c -o main.o main.cc
main.cc: In function `int main(int, char**)':
main.cc:19: error: conversion from `A<int>' to non-scalar type
`A<double>'
requested
make: *** [main.o] Error 1
 
Ad

Advertisements

P

PengYu.UT

The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.

Sorry, there were some typos in the previous post. Here is the
corrected code. Please look at the error line at the end of the
program.
I'm wondering why
A<double> b;
b = a;
works, but
A<double> c = a; //error
doesn't work.


/** source code */
template <typename U>
class A{
public:
A() { }
A &operator=(A &that) {
_internal = that._internal;
}
template <typename V>
A &operator=(V &that) {
_internal = that.get_internal();
}
U get_internal() const {return _internal; }
private:
U _internal;
};

int main(int argc, char *argv[])
{
A<int> a;
A<double> b;
b = a;
A<double> c = a; //error
}


/********* error message */
g++-3.3 -g -I/usr/local/include/clapack/ -c -o main.o main.cc
main.cc: In function `int main(int, char**)':
main.cc:22: error: conversion from `A<int>' to non-scalar type
`A<double>'
requested
make: *** [main.o] Error 1
 
K

Kai-Uwe Bux

The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.

Sorry, there were some typos in the previous post. Here is the
corrected code. Please look at the error line at the end of the
program.
I'm wondering why
A<double> b;
b = a;
works, but
A<double> c = a; //error
doesn't work.


/** source code */
template <typename U>
class A{
public:
A() { }
A &operator=(A &that) {
_internal = that._internal;
}
template <typename V>
A &operator=(V &that) {
_internal = that.get_internal();
}
U get_internal() const {return _internal; }
private:
U _internal;
};

int main(int argc, char *argv[])
{
A<int> a;
A<double> b;
b = a;
A<double> c = a; //error

Contrary to what the notation suggests, this does not call the assignment
operator. Instead a copy constructor is needed: C++ considers this line as
the construction of the object c from the object a. The above is actually
equivalent to:

A said:


Best

Kai-Uwe Bux
 
V

Victor Bazarov

Sorry, there were some typos in the previous post. Here is the
corrected code. Please look at the error line at the end of the
program.
I'm wondering why
A<double> b;
b = a;
works, but
A<double> c = a; //error
doesn't work.

Isn't this in the FAQ? There is no assignment operator involved
when you write

<type-id> name = othername;

It's called "initialisation" and the actual form is

<type-id> name(<type-id>(othername));

(only by form, the syntax immediately above won't work). IOW, you
attempt to initialise a temporary object of type 'A<double>' from
the 'a' object said:

V
 
Ad

Advertisements

V

Victor Bazarov

Kai-Uwe Bux said:
Contrary to what the notation suggests, this does not call the
assignment operator. Instead a copy constructor is needed: C++
considers this line as the construction of the object c from the
object a. The above is actually equivalent to:

A<double> c ( a );

Not exactly, but close enough.


V
 

Top