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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top