C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problems with template member functions in non-template clas

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jstarx@gmail.com
Guest





PostPosted: Sun Jun 12, 2005 9:50 pm    Post subject: Problems with template member functions in non-template clas Reply with quote



Hi, I'm having trouble understanding what's going on with my template
member functions. For example:

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

//testClass1
class testClass1
{
public:
testClass1(int x = 0)
{ data = x; }
friend ostream& operator <<(ostream& out, const testClass1& source)
{ out << source.data; return out; }
template testClass1& operator =(const numericType& rhs)
{ *this = testClass1(rhs); return *this; }
private:
int data;
};

//testClass2
class testClass2
{
public:
testClass2(int x = 0)
{ data = x; }
friend ostream& operator <<(ostream& out, const testClass2& source)
{ out << source.data; return out; }
template testClass2& operator =(const numericType& rhs);
private:
int data;
};

template <class numericType>
testClass2& testClass2::operator =(const numericType& rhs)
{ *this = testClass2(rhs); return *this; }

//Main program
int main(int argc, char* argv[])
{
testClass1 a(1);
testClass2 b(1);
cout << a << ',' << b << " - ";
a = 2;
b = 2;
cout << a << ',' << b << endl;
return 0;
}
////////////////////////////////////////////////////////

In VC++ 6.0 this compiles and runs fine. But the output is "1,1 -
2,1". I don't know why the assignment operator isn't functioning the
way it should in testClass2. The only difference between the two
classes is the overloaded assignment operator. One is inline, one has
a seperate implementation. But both functions use exactly the same
code, so why the difference?


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

Back to top
thoth39
Guest





PostPosted: Mon Jun 13, 2005 7:54 am    Post subject: Re: Problems with template member functions in non-template Reply with quote



Output with g++ 3.4 and g++ 4.0 is "1,1 - 2,2".

--
Pedro Lamarão


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

Back to top
Zedzed
Guest





PostPosted: Mon Jun 13, 2005 7:57 am    Post subject: Re: Problems with template member functions in non-template Reply with quote



this works correctly in VC++ 7.1 but I would have defined testClass1 as
follows:
class testClass1
{
public:
explicit testClass1(int x = 0)
{ data = x; }
testClass1& operator =(const testClass1& rhs)
{ data = rhs.data; return *this; }
testClass1& operator = (const int i) {
data = i; return *this; }
int getData() const
{
return data;
}
private:
int data;

};

ostream& operator <<(ostream& out, const testClass1& source)
{ out << source.getData(); return out; }

I am not sure I see the need for a template function.

and testClass2 in a similar way.


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

Back to top
Ismail Pazarbasi
Guest





PostPosted: Mon Jun 13, 2005 9:13 am    Post subject: Re: Problems with template member functions in non-template Reply with quote

Let me ask an awkward question; did you debug the code and see that it
hits assignment operator? If so, what are values before and after
function call?

Zedzed is right about class design. I did not test the code, but I
guess it works as expected.

Ismail


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

Back to top
Starx
Guest





PostPosted: Tue Jun 14, 2005 9:46 am    Post subject: Re: Problems with template member functions in non-template Reply with quote

The reason I am using a template function is because I would like my
constructor to be explicit but I would also like the assignment
operator to work with any data type that I have a constructor for. I
need the explicit to prevent the compiler from messing up the typecasts
and I'm overloading quite a few operators for quite a few data types,
I'd rather not write a function for each of them (it would be 80 or so
of them) so I was hoping a template function would do the trick.

I'm not sure how to use the debuger, but I did put output statements
into the assignment operator to see and they never get executed either.


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

Back to top
pavana.murthy@gmail.com
Guest





PostPosted: Tue Jun 14, 2005 7:35 pm    Post subject: Re: Problems with template member functions in non-template Reply with quote

Answer is that VC6 deos not 'fully' support "template member function"
in "non-template" class. [Although standard allows it ]
The fact that VC6 compiles the above program is a bug in VC6 compiler(
see MSDN for Standard non-conformance page)

The above non- conformance has been fixed in VC7 compiler.
Of course gcc supports it


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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.