Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   What the assignment operator from other type(with template) doesn't work? (http://www.velocityreviews.com/forums/t449655-what-the-assignment-operator-from-other-type-with-template-doesnt-work.html)

PengYu.UT@gmail.com 11-09-2005 03:15 AM

What the assignment operator from other type(with template) doesn't work?
 
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


PengYu.UT@gmail.com 11-09-2005 03:24 AM

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

PengYu.UT@gmail.com wrote:
> 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


Kai-Uwe Bux 11-09-2005 03:35 AM

Re: What the assignment operator from other type(with template) doesn't work?
 
PengYu.UT@gmail.com wrote:

>
> PengYu.UT@gmail.com wrote:
>> 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<double> c ( a );

> }



Best

Kai-Uwe Bux

Victor Bazarov 11-09-2005 03:46 AM

Re: What the assignment operator from other type(with template) doesn't work?
 
PengYu.UT@gmail.com wrote:
> PengYu.UT@gmail.com wrote:
>> 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.


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, when 'A<double>' has no constructor from 'A<int>'.

> [...]


V



Victor Bazarov 11-09-2005 03:47 AM

Re: What the assignment operator from other type(with template) doesn't work?
 
Kai-Uwe Bux wrote:
>> 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<double> c ( a );


Not exactly, but close enough.


>
>> }

>


V




All times are GMT. The time now is 03:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.