Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > template proble...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 9750 of 10094
Post > Topic >>

template problem

by Bo <bo.zhang0609@[EMAIL PROTECTED] > Jun 27, 2008 at 02:24 PM

Hi all,

Below is a short program where I define a class A, in which I redefine
the operator =.

Inside the operator implementation, it just prints a line to notify
that it is indeed the new assignment operator which is called.

In main(), I define two variables a and b, both of the same type
A<int>. Then I write "a = b", where I expect that the new operator=
will be called. I expect that the compiler can figure out that "T =
int" and "S = int".

However, this is not the case. As the program is running, the new
operator is not called. It is the default operator= which is used.
Now, if I change the definition of "b" to "A<float> b", then the new
operator= is used.

Why the compiler cannot figure out in the first case that "T = int"
and "S = int" ?

------------------------
#include <iostream>
using namespace std;

template <class T>
class A
{
public:
	template <class S>
	A<T>& operator = (const A<S>& a)
	{
		cerr << "inside operator =" << endl;
		return *this;
	}
};

int main()
{
	A<int> a;
	A<int> b;

	a = b; // the redefined assingment operator is NOT called.
                  // If I declare A<float> b, then the new operator is
called.
}
-------------------------------

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 6 Posts in Topic:
template problem
Bo <bo.zhang0609@[EMAI  2008-06-27 14:24:59 
Re: template problem
Thomas Maeder <maeder@  2008-06-28 01:28:19 
Re: template problem
Greg Herlihy <greghe@[  2008-06-28 01:28:11 
Re: template problem
Hoobert <michal_hr@[EM  2008-06-28 01:27:59 
Re: template problem
Chris Uzdavinis <cuzda  2008-06-28 01:28:01 
Re: template problem
Francis Glassborow <fr  2008-06-28 16:58:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 22:18:06 CDT 2008.