| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"none" |
| Date: |
10 Jan 2008 06:47:22 AM |
| Object: |
operator overloading question |
I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:
intvariable = fooclass;
here's a pseudo code of what I'm doing:
template <class COMPLEXTYPE>
class foo
{
public:
COMPLEXTYPE data;
foo (void) { }
~foo (void) { }
foo<COMPLEXTYPE> operator = (foo<COMPLEXTYPE> source);
{
this->data = source.data;
return *this;
}
foo<COMPLEXTYPE> operator = (COMPLEXTYPE source);
{
this->data = source;
return *this;
}
}
// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE> source)
{
dest = (COMPLEXTYPE) source.data;
return dest;
}
main (void)
{
foo<int> FOOINT;
foo<int> FOOINT2;
int integer;
FOOTINT.data = 1;
integer = 4;
FOOINT2 = FOOINT; // works
FOOINT = integer; // works
integer = FOOINT2; // does not work
}
Anyone knows what's wrong? I know we can't overload
the basic operators but I thought I could do something
like that. Thanks in advance
.
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 02:53:24 AM |
|
|
I was able to do what I wanted, but not exactly the way I would
like it to work. I choose this method because I don't want to
make 3 copies for each operators overload (int,complex), (complex,int)
and (complex,complex). This way I only have to build
a function for each operators.
I'm able to do this:
COMPLEX complex;
COMPLEX complex2;
complex.real = 1;
complex.img = 3;
complex2 = (COMPLEX) 3 + complex;
complex = complex2 + (COMPLEX) 5;
but I would like to do:
complex2 = 3 + complex;
complex = complex2 + 5;
but I get this error:
error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +(class
Ccomplex<COMPLEXTYPE> &,class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for 'class Ccomplex<COMPLEXTYPE> &' from
'const int'
Anyone knows how I could do what I want to do?
By the way I forgot about:
int integer = complex; // integer = complex.real
because I think Daniel T. is right and it's not a smart thing
to do and can be very confusing.
here's a sample of my code :
// mycomplex.h
template <class COMPLEXTYPE> class Ccomplex;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
Ccomplex<COMPLEXTYPE>
&source);
// also tried
// inline Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE>
dest, \
// Ccomplex<COMPLEXTYPE>
source);
template <class COMPLEXTYPE>
class Ccomplex
{
public:
COMPLEXTYPE real;
COMPLEXTYPE img;
// Constructor
Ccomplex (void)
{
real = 0;
img = 0;
}
// Constructor (conversion operator)
Ccomplex (COMPLEXTYPE val)
{
real = val;
img = 0;
}
// Destructor
~Ccomplex (void) { }
inline Ccomplex<COMPLEXTYPE> operator = (Ccomplex<COMPLEXTYPE>
source);
friend inline Ccomplex<COMPLEXTYPE>& operator +
(Ccomplex<COMPLEXTYPE> &dest, \
Ccomplex<COMPLEXTYPE> &source);
// also tried
// friend inline Ccomplex<COMPLEXTYPE> operator +
(Ccomplex<COMPLEXTYPE> dest, \
//
Ccomplex<COMPLEXTYPE> source);
protected:
};
typedef Ccomplex<int> COMPLEX;
// mycomplex.cpp
#include "complex.h"
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> Ccomplex<COMPLEXTYPE>::operator =
(Ccomplex<COMPLEXTYPE> source)
{
this->real = source.real;
this->img = source.img;
return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
Ccomplex<COMPLEXTYPE>
&source)
{
dest.real = dest.real + source.real;
dest.img = dest.img + source.img;
return dest;
}
template class Ccomplex<int>;
template Ccomplex<int>& operator + (Ccomplex<int> &dest, \
Ccomplex<int> &source);
// program.cpp
#include "complex.h"
int main(FT_INT argc, FT_CHAR* argv[])
{
KCOMPLEX complex;
KCOMPLEX complex2;
complex.real = 5;
complex.img = 2;
// WORKS
complex2 = (COMPLEX) 1 + complex;
complex = complex2 + (COMPLEX) 2;
// DO NOT WORK
// error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +
(class Ccomplex<COMPLEXTYPE> &,
// class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for
// 'class Ccomplex<COMPLEXTYPE> &' from 'const int'
complex2 = 1 + complex;
complex = complex2 + 2;
return 0;
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 03:50:08 AM |
|
|
I made some modification to my code because I noticed
some errors. I have removed every & for the operator +
and added the newdest temp object because I the old code
changed the value of the dest object, and I do not want
that.
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE> dest,
Ccomplex<COMPLEXTYPE> source)
{
Ccomplex<COMPLEXTYPE> newdest;
newdest.real = dest.real + source.real;
newdest.img = dest.img + source.img;
return newdest;
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 04:06:46 AM |
|
|
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator = (COMPLEXTYPE source)
{
this->real = source;
this->img = 0;
return *this;
}
.
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
11 Jan 2008 05:07:55 AM |
|
|
none <mikem891@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator = (COMPLEXTYPE source)
{
this->real = source;
this->img = 0;
return *this;
}
Why are you throwing away the imaginary part? Why all the copies? Why
even write the function when the compiler supplied op= does the right
thing?
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 05:27:17 AM |
|
|
On Jan 11, 6:07=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
{
=A0 this->real =3D source;
=A0 this->img =A0=3D 0;
=A0 return *this;
}
Why are you throwing away the imaginary part? Why all the copies? Why
even write the function when the compiler supplied op=3D does the right
thing?
because it's a complex number =3D a real number;
complex =3D real =3D> complex =3D real + 0i
the 2 copies are there to provide
complex =3D complex and complex =3D real
I'm currently trying to figure out what's wrong with this code
so maybe there's some useless parts for the moment.
isn't compiler supplied op=3D only for complex =3D complex?
.
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
11 Jan 2008 10:52:34 AM |
|
|
none <mikem891@hotmail.com> wrote:
On Jan 11, 6:07 am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator = (COMPLEXTYPE source)
{
this->real = source;
this->img = 0;
return *this;
}
Why are you throwing away the imaginary part? Why all the copies? Why
even write the function when the compiler supplied op= does the right
thing?
because it's a complex number = a real number;
complex = real => complex = real + 0i
the 2 copies are there to provide
complex = complex and complex = real
I'm currently trying to figure out what's wrong with this code
so maybe there's some useless parts for the moment.
isn't compiler supplied op= only for complex = complex?
Right, my mistake.
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 11:42:40 AM |
|
|
On Jan 11, 11:52=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
On Jan 11, 6:07=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
{
=A0 this->real =3D source;
=A0 this->img =A0=3D 0;
=A0 return *this;
}
Why are you throwing away the imaginary part? Why all the copies? Why
even write the function when the compiler supplied op=3D does the righ=
t
thing?
because it's a complex number =3D a real number;
complex =3D real =3D> complex =3D real + 0i
the 2 copies are there to provide
complex =3D complex and complex =3D real
I'm currently trying to figure out what's wrong with this code
so maybe there's some useless parts for the moment.
isn't compiler supplied op=3D only for complex =3D complex?
Right, my mistake.- Hide quoted text -
- Show quoted text -
Ok, I was able to make it work the way I wanted to work, this
code now works fine:
complex2 =3D 1 + complex1;
complex1 =3D complex2 + 2;
And now I do not have to add 3 overloads for each operators,
only one is fine.
I had to add this conversion operator:
operator COMPLEXTYPE ()
{
return real;
}
I'm not sure why it make it work, because I don't see where
Ccomplex<COMPLEXTYPE> is converted to COMPLEXTYPE.
What do you think does it look fine to you?
Here's the new code:
------------------------------------------------------------
template <class COMPLEXTYPE>
class Ccomplex
{
public:
COMPLEXTYPE real;
COMPLEXTYPE img;
// Constructor
Ccomplex (void)
{
real =3D 0;
img =3D 0;
}
// Constructor (conversion operator)
Ccomplex (COMPLEXTYPE val)
{
real =3D val;
img =3D 0;
}
// convert Ccomplex to real (COMPLEXTYPE)
operator COMPLEXTYPE ()
{
return real;
}
// Destructor
~Ccomplex (void) { }
// Ccomplex =3D Ccomplex overload
inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE>
source)
{
this->real =3D source.real;
this->img =3D source.img;
return *this;
}
// Ccomplex =3D real (COMPLEXTYPE) overload
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
{
this->real =3D source;
this->img =3D 0;
return *this;
}
friend inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, \
Ccomplex<COMPLEXTYPE> source);
protected:
};
typedef Ccomplex<double> COMPLEX;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> source)
{
Ccomplex<COMPLEXTYPE> newdest;
newdest.real =3D dest.real + source.real;
newdest.img =3D dest.img + source.img;
return newdest;
}
int main(int argc, char* argv[])
{
COMPLEX complex1;
COMPLEX complex2;
complex1.real =3D 5;
complex1.img =3D 2;
// WORKS
complex2 =3D complex1;
// WORKS
complex2 =3D (COMPLEX) 1 + complex1;
complex1 =3D complex2 + (COMPLEX) 2;
// WORKS
complex2 =3D 1 + complex1;
complex1 =3D complex2 + 2;
return 0;
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 12:03:45 PM |
|
|
On Jan 11, 12:42=A0pm, none <mikem...@hotmail.com> wrote:
On Jan 11, 11:52=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
On Jan 11, 6:07=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
{
=A0 this->real =3D source;
=A0 this->img =A0=3D 0;
=A0 return *this;
}
Why are you throwing away the imaginary part? Why all the copies? Wh=
y
even write the function when the compiler supplied op=3D does the ri=
ght
thing?
because it's a complex number =3D a real number;
complex =3D real =3D> complex =3D real + 0i
the 2 copies are there to provide
complex =3D complex and complex =3D real
I'm currently trying to figure out what's wrong with this code
so maybe there's some useless parts for the moment.
isn't compiler supplied op=3D only for complex =3D complex?
Right, my mistake.- Hide quoted text -
- Show quoted text -
Ok, I was able to make it work the way I wanted to work, this
code now works fine:
=A0 =A0 complex2 =3D 1 + complex1;
=A0 =A0 complex1 =3D complex2 + 2;
And now I do not have to add 3 overloads for each operators,
only one is fine.
I had to add this conversion operator:
=A0 =A0operator COMPLEXTYPE ()
=A0 =A0{
=A0 =A0 return real;
=A0 =A0}
I'm not sure why it make it work, because I don't see where
Ccomplex<COMPLEXTYPE> is converted to COMPLEXTYPE.
What do you think does it look fine to you?
Here's the new code:
------------------------------------------------------------
template <class COMPLEXTYPE>
class Ccomplex
{
=A0public:
=A0 =A0COMPLEXTYPE real;
=A0 =A0COMPLEXTYPE img;
=A0 =A0// Constructor
=A0 =A0Ccomplex (void)
=A0 =A0{
=A0 =A0 real =3D 0;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// Constructor (conversion operator)
=A0 =A0Ccomplex (COMPLEXTYPE val)
=A0 =A0{
=A0 =A0 real =3D val;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// convert Ccomplex to real (COMPLEXTYPE)
=A0 =A0operator COMPLEXTYPE ()
=A0 =A0{
=A0 =A0 return real;
=A0 =A0}
=A0 =A0// Destructor
=A0 =A0~Ccomplex (void) { }
=A0 =A0// Ccomplex =3D Ccomplex overload
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE>
source)
=A0 =A0{
=A0 =A0 this->real =3D source.real;
=A0 =A0 this->img =A0=3D source.img;
=A0 =A0 return *this;
=A0 =A0}
=A0 =A0// Ccomplex =3D real (COMPLEXTYPE) overload
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
=A0 =A0{
=A0 =A0 this->real =3D source;
=A0 =A0 this->img =A0=3D 0;
=A0 =A0 return *this;
=A0 =A0}
=A0 =A0friend inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest, \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Ccomplex<COMPLEXTYPE> source);
=A0protected:
};
typedef Ccomplex<double> COMPLEX;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> source)
{
=A0 Ccomplex<COMPLEXTYPE> newdest;
=A0 newdest.real =3D dest.real + source.real;
=A0 newdest.img =A0=3D dest.img =A0+ source.img;
=A0 return newdest;
}
int main(int argc, char* argv[])
{
=A0 =A0 COMPLEX complex1;
=A0 =A0 COMPLEX complex2;
=A0 =A0 complex1.real =3D 5;
=A0 =A0 complex1.img =A0=3D 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D complex1;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D (COMPLEX) 1 + complex1;
=A0 =A0 complex1 =3D complex2 + (COMPLEX) 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D 1 + complex1;
=A0 =A0 complex1 =3D complex2 + 2;
=A0 =A0 return 0;
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
I just found out a bug in my code, a these lines:
complex2 =3D 1 + complex1;
complex1 =3D complex2 + 2;
The imaginary part is set to 0, I'm currently trying to figure out
why.
if complex2 =3D 1+2i it should be:
complex1 =3D 1+2i + 2+0i =3D 3+2i
but in my code it's 3+0i.
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 12:14:08 PM |
|
|
On Jan 11, 1:03=A0pm, none <mikem...@hotmail.com> wrote:
On Jan 11, 12:42=A0pm, none <mikem...@hotmail.com> wrote:
On Jan 11, 11:52=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
On Jan 11, 6:07=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
{
=A0 this->real =3D source;
=A0 this->img =A0=3D 0;
=A0 return *this;
}
Why are you throwing away the imaginary part? Why all the copies? =
Why
even write the function when the compiler supplied op=3D does the =
right
thing?
because it's a complex number =3D a real number;
complex =3D real =3D> complex =3D real + 0i
the 2 copies are there to provide
complex =3D complex and complex =3D real
I'm currently trying to figure out what's wrong with this code
so maybe there's some useless parts for the moment.
isn't compiler supplied op=3D only for complex =3D complex?
Right, my mistake.- Hide quoted text -
- Show quoted text -
Ok, I was able to make it work the way I wanted to work, this
code now works fine:
=A0 =A0 complex2 =3D 1 + complex1;
=A0 =A0 complex1 =3D complex2 + 2;
And now I do not have to add 3 overloads for each operators,
only one is fine.
I had to add this conversion operator:
=A0 =A0operator COMPLEXTYPE ()
=A0 =A0{
=A0 =A0 return real;
=A0 =A0}
I'm not sure why it make it work, because I don't see where
Ccomplex<COMPLEXTYPE> is converted to COMPLEXTYPE.
What do you think does it look fine to you?
Here's the new code:
------------------------------------------------------------
template <class COMPLEXTYPE>
class Ccomplex
{
=A0public:
=A0 =A0COMPLEXTYPE real;
=A0 =A0COMPLEXTYPE img;
=A0 =A0// Constructor
=A0 =A0Ccomplex (void)
=A0 =A0{
=A0 =A0 real =3D 0;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// Constructor (conversion operator)
=A0 =A0Ccomplex (COMPLEXTYPE val)
=A0 =A0{
=A0 =A0 real =3D val;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// convert Ccomplex to real (COMPLEXTYPE)
=A0 =A0operator COMPLEXTYPE ()
=A0 =A0{
=A0 =A0 return real;
=A0 =A0}
=A0 =A0// Destructor
=A0 =A0~Ccomplex (void) { }
=A0 =A0// Ccomplex =3D Ccomplex overload
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE>
source)
=A0 =A0{
=A0 =A0 this->real =3D source.real;
=A0 =A0 this->img =A0=3D source.img;
=A0 =A0 return *this;
=A0 =A0}
=A0 =A0// Ccomplex =3D real (COMPLEXTYPE) overload
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
=A0 =A0{
=A0 =A0 this->real =3D source;
=A0 =A0 this->img =A0=3D 0;
=A0 =A0 return *this;
=A0 =A0}
=A0 =A0friend inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest, \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Ccomplex<COMPLEXTYPE> source);
=A0protected:
};
typedef Ccomplex<double> COMPLEX;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> source=
)
{
=A0 Ccomplex<COMPLEXTYPE> newdest;
=A0 newdest.real =3D dest.real + source.real;
=A0 newdest.img =A0=3D dest.img =A0+ source.img;
=A0 return newdest;
}
int main(int argc, char* argv[])
{
=A0 =A0 COMPLEX complex1;
=A0 =A0 COMPLEX complex2;
=A0 =A0 complex1.real =3D 5;
=A0 =A0 complex1.img =A0=3D 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D complex1;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D (COMPLEX) 1 + complex1;
=A0 =A0 complex1 =3D complex2 + (COMPLEX) 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D 1 + complex1;
=A0 =A0 complex1 =3D complex2 + 2;
=A0 =A0 return 0;
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
I just found out a bug in my code, a these lines:
complex2 =3D 1 + complex1;
complex1 =3D complex2 + 2;
The imaginary part is set to 0, I'm currently trying to figure out
why.
if complex2 =3D 1+2i it should be:
complex1 =3D 1+2i + 2+0i =3D 3+2i
but in my code it's 3+0i.- Hide quoted text -
- Show quoted text -
I found out the problem, but I cannot remove this line
because I won't be able to do something like this:
Ccomplex complex1 =3D 3; // complex1 =3D 3+0i
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
{
this->real =3D source;
// THIS IS THE PROBLEM:
this->img =3D 0;
return *this;
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 01:34:45 PM |
|
|
On Jan 11, 1:14=A0pm, none <mikem...@hotmail.com> wrote:
On Jan 11, 1:03=A0pm, none <mikem...@hotmail.com> wrote:
On Jan 11, 12:42=A0pm, none <mikem...@hotmail.com> wrote:
On Jan 11, 11:52=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
On Jan 11, 6:07=A0am, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
And I have added this assignment operator as a member
of the class:
inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)=
{
=A0 this->real =3D source;
=A0 this->img =A0=3D 0;
=A0 return *this;
}
Why are you throwing away the imaginary part? Why all the copies=
? Why
even write the function when the compiler supplied op=3D does th=
e right
thing?
because it's a complex number =3D a real number;
complex =3D real =3D> complex =3D real + 0i
the 2 copies are there to provide
complex =3D complex and complex =3D real
I'm currently trying to figure out what's wrong with this code
so maybe there's some useless parts for the moment.
isn't compiler supplied op=3D only for complex =3D complex?
Right, my mistake.- Hide quoted text -
- Show quoted text -
Ok, I was able to make it work the way I wanted to work, this
code now works fine:
=A0 =A0 complex2 =3D 1 + complex1;
=A0 =A0 complex1 =3D complex2 + 2;
And now I do not have to add 3 overloads for each operators,
only one is fine.
I had to add this conversion operator:
=A0 =A0operator COMPLEXTYPE ()
=A0 =A0{
=A0 =A0 return real;
=A0 =A0}
I'm not sure why it make it work, because I don't see where
Ccomplex<COMPLEXTYPE> is converted to COMPLEXTYPE.
What do you think does it look fine to you?
Here's the new code:
------------------------------------------------------------
template <class COMPLEXTYPE>
class Ccomplex
{
=A0public:
=A0 =A0COMPLEXTYPE real;
=A0 =A0COMPLEXTYPE img;
=A0 =A0// Constructor
=A0 =A0Ccomplex (void)
=A0 =A0{
=A0 =A0 real =3D 0;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// Constructor (conversion operator)
=A0 =A0Ccomplex (COMPLEXTYPE val)
=A0 =A0{
=A0 =A0 real =3D val;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// convert Ccomplex to real (COMPLEXTYPE)
=A0 =A0operator COMPLEXTYPE ()
=A0 =A0{
=A0 =A0 return real;
=A0 =A0}
=A0 =A0// Destructor
=A0 =A0~Ccomplex (void) { }
=A0 =A0// Ccomplex =3D Ccomplex overload
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE=
source)
=A0 =A0{
=A0 =A0 this->real =3D source.real;
=A0 =A0 this->img =A0=3D source.img;
=A0 =A0 return *this;
=A0 =A0}
=A0 =A0// Ccomplex =3D real (COMPLEXTYPE) overload
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
=A0 =A0{
=A0 =A0 this->real =3D source;
=A0 =A0 this->img =A0=3D 0;
=A0 =A0 return *this;
=A0 =A0}
=A0 =A0friend inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest, \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Ccomplex<COMPLEXTYPE> source);
=A0protected:
};
typedef Ccomplex<double> COMPLEX;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> sour=
ce)
{
=A0 Ccomplex<COMPLEXTYPE> newdest;
=A0 newdest.real =3D dest.real + source.real;
=A0 newdest.img =A0=3D dest.img =A0+ source.img;
=A0 return newdest;
}
int main(int argc, char* argv[])
{
=A0 =A0 COMPLEX complex1;
=A0 =A0 COMPLEX complex2;
=A0 =A0 complex1.real =3D 5;
=A0 =A0 complex1.img =A0=3D 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D complex1;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D (COMPLEX) 1 + complex1;
=A0 =A0 complex1 =3D complex2 + (COMPLEX) 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D 1 + complex1;
=A0 =A0 complex1 =3D complex2 + 2;
=A0 =A0 return 0;
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
I just found out a bug in my code, a these lines:
complex2 =3D 1 + complex1;
complex1 =3D complex2 + 2;
The imaginary part is set to 0, I'm currently trying to figure out
why.
if complex2 =3D 1+2i it should be:
complex1 =3D 1+2i + 2+0i =3D 3+2i
but in my code it's 3+0i.- Hide quoted text -
- Show quoted text -
I found out the problem, but I cannot remove this line
because I won't be able to do something like this:
Ccomplex complex1 =3D 3; // complex1 =3D 3+0i
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (COMPLEXTYPE source)
=A0 =A0{
=A0 =A0 this->real =3D source;
=A0 =A0 // THIS IS THE PROBLEM:
=A0 =A0 this->img =A0=3D 0;
=A0 =A0 return *this;
=A0 =A0}- Hide quoted text -
- Show quoted text -
the problem is that
Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE> dest,
Ccomplex<COMPLEXTYPE> source);
never gets called at these line:
complex2 =3D 1 + complex1;
complex1 =3D complex2 + 2;
shouldn't the const be converted to a Ccomplex object? I can make
the code work correctly if I add these lines:
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE> source)
{ return (Ccomplex<COMPLEXTYPE>) dest + source; }
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, COMPLEXTYPE source)
{ return dest + (Ccomplex<COMPLEXTYPE>) source; }
but then again I'm back with 3 overloads for every
operators.
.
|
|
|
|
|
|
|
|
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
11 Jan 2008 07:49:23 AM |
|
|
In article
<b28d82be-d722-462f-9452-a0557a08d871@d70g2000hsb.googlegroups.com>,
none <mikem891@hotmail.com> wrote:
I was able to do what I wanted, but not exactly the way I would
like it to work. I choose this method because I don't want to
make 3 copies for each operators overload (int,complex), (complex,int)
and (complex,complex). This way I only have to build
a function for each operators.
I'm able to do this:
COMPLEX complex;
COMPLEX complex2;
complex.real = 1;
complex.img = 3;
complex2 = (COMPLEX) 3 + complex;
complex = complex2 + (COMPLEX) 5;
but I would like to do:
complex2 = 3 + complex;
complex = complex2 + 5;
template < typename T >
class Complex
{
public:
Complex(): real(0), imag(0) { }
Complex( T r, T i ): real(r), imag(i) { }
T real;
T imag;
};
template < typename T >
Complex<T> operator+( T left, const Complex<T>& right )
{
return Complex<T>( left + right.real, right.imag );
}
template < typename T >
Complex<T> operator+( const Complex<T>& left, T right )
{
return Complex<T>( left.real + right, left.imag );
}
int main(int argc, char* argv[])
{
Complex<int> complex;
Complex<int> complex2;
complex.real = 5;
complex.imag = 2;
complex2 = 1 + complex;
assert( complex2.real == 6 );
assert( complex2.imag == 2 );
complex2 = complex + 1;
assert( complex2.real == 6 );
assert( complex2.imag == 2 );
}
but I get this error:
error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +(class
Ccomplex<COMPLEXTYPE> &,class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for 'class Ccomplex<COMPLEXTYPE> &' from
'const int'
Anyone knows how I could do what I want to do?
By the way I forgot about:
int integer = complex; // integer = complex.real
because I think Daniel T. is right and it's not a smart thing
to do and can be very confusing.
here's a sample of my code :
Unfortunately, your code is riddled with errors. Try posting your actual
code and I'll be happy to review it. In general though, you are making
this way harder on yourself than it has to be. There are all kinds of
unnecessary constructs in your code, and other stuff that is missing but
must be there.
// mycomplex.h
template <class COMPLEXTYPE> class Ccomplex;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
Ccomplex<COMPLEXTYPE>
&source);
// also tried
// inline Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE>
dest, \
// Ccomplex<COMPLEXTYPE>
source);
template <class COMPLEXTYPE>
class Ccomplex
{
public:
COMPLEXTYPE real;
COMPLEXTYPE img;
// Constructor
Ccomplex (void)
{
real = 0;
img = 0;
}
// Constructor (conversion operator)
Ccomplex (COMPLEXTYPE val)
{
real = val;
img = 0;
}
// Destructor
~Ccomplex (void) { }
inline Ccomplex<COMPLEXTYPE> operator = (Ccomplex<COMPLEXTYPE>
source);
friend inline Ccomplex<COMPLEXTYPE>& operator +
(Ccomplex<COMPLEXTYPE> &dest, \
Ccomplex<COMPLEXTYPE> &source);
// also tried
// friend inline Ccomplex<COMPLEXTYPE> operator +
(Ccomplex<COMPLEXTYPE> dest, \
//
Ccomplex<COMPLEXTYPE> source);
protected:
};
typedef Ccomplex<int> COMPLEX;
// mycomplex.cpp
#include "complex.h"
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> Ccomplex<COMPLEXTYPE>::operator =
(Ccomplex<COMPLEXTYPE> source)
{
this->real = source.real;
this->img = source.img;
return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
Ccomplex<COMPLEXTYPE>
&source)
{
dest.real = dest.real + source.real;
dest.img = dest.img + source.img;
return dest;
}
template class Ccomplex<int>;
Spurious ';'
template Ccomplex<int>& operator + (Ccomplex<int> &dest, \
Ccomplex<int> &source);
Function above declared but not defined. It's wholly unnecessary anyway.
// program.cpp
#include "complex.h"
int main(FT_INT argc, FT_CHAR* argv[])
FT_INT and FT_CHAR were not declared. Use 'int' and 'char' instead.
{
KCOMPLEX complex;
KCOMPLEX complex2;
KCOMPLEX was not declared. I changed them to COMPLEX.
complex.real = 5;
complex.img = 2;
// WORKS
complex2 = (COMPLEX) 1 + complex;
The above doesn't work. You are creating a temporary and because your
op+ takes its parameters by non-const reference, it can't be used.
complex = complex2 + (COMPLEX) 2;
// DO NOT WORK
// error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +
(class Ccomplex<COMPLEXTYPE> &,
// class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for
// 'class Ccomplex<COMPLEXTYPE> &' from 'const int'
complex2 = 1 + complex;
complex = complex2 + 2;
return 0;
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 10:08:56 AM |
|
|
On Jan 11, 8:49=A0am, "Daniel T." <danie...@earthlink.net> wrote:
In article
<b28d82be-d722-462f-9452-a0557a08d...@d70g2000hsb.googlegroups.com>,
=A0none <mikem...@hotmail.com> wrote:
I was able to do what I wanted, but not exactly the way I would
like it to work. I choose this method because I don't want to
make 3 copies for each operators overload (int,complex), (complex,int)
and (complex,complex). This way I only have to build
a function for each operators.
I'm able to do this:
COMPLEX complex;
COMPLEX complex2;
complex.real =3D 1;
complex.img =A0=3D 3;
complex2 =3D (COMPLEX) 3 + complex;
complex =A0=3D complex2 =A0 =A0+ (COMPLEX) 5;
but I would like to do:
complex2 =3D 3 + complex;
complex =A0=3D complex2 + 5;
=A0 =A0template < typename T >
class Complex
{
public:
=A0 =A0Complex(): real(0), imag(0) { }
=A0 =A0Complex( T r, T i ): real(r), imag(i) { }
=A0 =A0T real;
=A0 =A0T imag;
};
template < typename T >
Complex<T> operator+( T left, const Complex<T>& right )
{
=A0 =A0return Complex<T>( left + right.real, right.imag );
}
template < typename T >
Complex<T> operator+( const Complex<T>& left, T right )
{
=A0 =A0return Complex<T>( left.real + right, left.imag );
}
int main(int argc, char* argv[])
{
=A0 =A0Complex<int> complex;
=A0 =A0Complex<int> complex2;
=A0 =A0complex.real =3D 5;
=A0 =A0complex.imag =A0=3D 2;
=A0 =A0complex2 =3D 1 + complex;
=A0 =A0assert( complex2.real =3D=3D 6 );
=A0 =A0assert( complex2.imag =3D=3D 2 );
=A0 =A0complex2 =3D complex + 1;
=A0 =A0assert( complex2.real =3D=3D 6 );
=A0 =A0assert( complex2.imag =3D=3D 2 );
}
but I get this error:
error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +(class
Ccomplex<COMPLEXTYPE> &,class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for 'class Ccomplex<COMPLEXTYPE> &' from
'const int'
Anyone knows how I could do what I want to do?
By the way I forgot about:
int integer =3D complex; // integer =3D complex.real
because I think Daniel T. is right and it's not a smart thing
to do and can be very confusing.
here's a sample of my code :
Unfortunately, your code is riddled with errors. Try posting your actual
code and I'll be happy to review it. In general though, you are making
this way harder on yourself than it has to be. There are all kinds of
unnecessary constructs in your code, and other stuff that is missing but
must be there.
// mycomplex.h
template <class COMPLEXTYPE> class Ccomplex;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 Ccomplex<COMPLEXTYPE>
&source);
// also tried
// inline Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE>
dest, \
// =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0Ccomplex<COMPLEXTYPE>
source);
template <class COMPLEXTYPE>
class Ccomplex
{
=A0public:
=A0 =A0COMPLEXTYPE real;
=A0 =A0COMPLEXTYPE img;
=A0 =A0// Constructor
=A0 =A0Ccomplex (void)
=A0 =A0{
=A0 =A0 real =3D 0;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// Constructor (conversion operator)
=A0 =A0Ccomplex (COMPLEXTYPE val)
=A0 =A0{
=A0 =A0 real =3D val;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
=A0 =A0// Destructor
=A0 =A0~Ccomplex (void) { }
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE>
source);
=A0 =A0friend inline Ccomplex<COMPLEXTYPE>& operator +
(Ccomplex<COMPLEXTYPE> &dest, \
Ccomplex<COMPLEXTYPE> &source);
=A0 =A0// also tried
=A0 =A0// friend inline Ccomplex<COMPLEXTYPE> operator +
(Ccomplex<COMPLEXTYPE> dest, \
=A0 =A0//
Ccomplex<COMPLEXTYPE> source);
=A0protected:
};
typedef Ccomplex<int> COMPLEX;
// mycomplex.cpp
#include "complex.h"
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> Ccomplex<COMPLEXTYPE>::operator =3D
(Ccomplex<COMPLEXTYPE> source)
{
=A0 this->real =3D source.real;
=A0 this->img =A0=3D source.img;
=A0 return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 Ccomplex<COMPLEXTYPE>
&source)
{
=A0 dest.real =3D dest.real + source.real;
=A0 dest.img =A0=3D dest.img =A0+ source.img;
=A0 return dest;
}
template class Ccomplex<int>;
Spurious ';'
template Ccomplex<int>& operator + (Ccomplex<int> &dest, \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
Ccomplex<int> &source);
Function above declared but not defined. It's wholly unnecessary anyway.
// program.cpp
#include "complex.h"
int main(FT_INT argc, FT_CHAR* argv[])
FT_INT and FT_CHAR were not declared. Use 'int' and 'char' instead.
{
=A0 =A0 KCOMPLEX complex;
=A0 =A0 KCOMPLEX complex2;
KCOMPLEX was not declared. I changed them to COMPLEX.
=A0 =A0 complex.real =3D 5;
=A0 =A0 complex.img =A0=3D 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D (COMPLEX) 1 + complex;
The above doesn't work. You are creating a temporary and because your
op+ takes its parameters by non-const reference, it can't be used.
=A0 =A0 complex =A0=3D complex2 + (COMPLEX) 2;
=A0 =A0 // DO NOT WORK
=A0 =A0 // error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +=
(class Ccomplex<COMPLEXTYPE> &,
=A0 =A0 // =A0 =A0 =A0 =A0 =A0 =A0 =A0 class Ccomplex<COMPLEXTYPE> &)' :=
could not
deduce template argument for
=A0 =A0 // =A0 =A0 =A0 =A0 =A0 =A0 =A0'class Ccomplex<COMPLEXTYPE> &' fr=
om 'const int'
=A0 =A0 complex2 =3D 1 + complex;
=A0 =A0 complex =A0=3D complex2 + 2;
=A0 =A0 return 0;
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Sorry about the errors, I didn't test before posting it. Here's
a new working, cleaned and tested code. I have removed a lot
of things to make it simplier and I do not use multiple files
anymore for simplicity.
Now this code works fine like this:
COMPLEX complex1,complex2;
complex1 =3D 2.0 + complex2;
complex2 =3D complex1 + 4.0;
But I have to use a double notation to prevent compiler error
(ambiguity between int and double), I would like to be able to
write it as 1 + complex. And I also need to declare these
operator + overloads to type cast the values:
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, const COMPLEXTYPE source);
inline Ccomplex<COMPLEXTYPE> operator + \
(const COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE> source);
I'm sure there's a better way to do this, I would really like
to be able to declare only one operator overload for each
operators. By the way I think I'm may be missing some important
points about templates, I find the class and typename keywords
really confusion. Should I use typename for member types and
class only for objects? (there's no informations about
templates in any of my C++ books)
for example:
template <class COMPLEXTYPE, typename TYPENAME>
class Ccomplex
{
public:
TYPENAME real;
TYPENAME img;
=2E..
inline Ccomplex<COMPLEXTYPE,TYPENAME> operator =3D \
(Ccomplex<COMPLEXTYPE,TYPENAME> source);
=2E..
}
or am I doing it the right way?
thanks a lot for your help, your are really helpful!
// ------------------------------------------------------------
template <class COMPLEXTYPE>
class Ccomplex
{
public:
COMPLEXTYPE real;
COMPLEXTYPE img;
// Constructor
Ccomplex (void)
{
real =3D 0;
img =3D 0;
}
// Constructor (conversion operator)
Ccomplex (COMPLEXTYPE val)
{
real =3D val;
img =3D 0;
}
// Destructor
~Ccomplex (void) { }
inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE>
source);
friend inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE>
source);
friend inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, const COMPLEXTYPE
source);
friend inline Ccomplex<COMPLEXTYPE> operator + \
(const COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE>
source);
protected:
};
typedef Ccomplex<double> COMPLEX;
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> \
Ccomplex<COMPLEXTYPE>::operator =3D (Ccomplex<COMPLEXTYPE>
source)
{
this->real =3D source.real;
this->img =3D source.img;
return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> source)
{
Ccomplex<COMPLEXTYPE> newdest;
newdest.real =3D dest.real + source.real;
newdest.img =3D dest.img + source.img;
return newdest;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest,const COMPLEXTYPE source)
{ return dest + (Ccomplex<COMPLEXTYPE>) source; }
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(const COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE> source)
{ return (Ccomplex<COMPLEXTYPE>) dest + source; }
int main(int argc, char* argv[])
{
COMPLEX complex;
COMPLEX complex2;
complex.real =3D 5;
complex.img =3D 2;
// WORKS
complex2 =3D (COMPLEX) 1 + complex;
complex =3D complex2 + (COMPLEX) 2;
// WORKS but must use double notation to avoid
// template ambiguities between in and double
complex2 =3D 1.0 + complex;
complex =3D complex2 + 2.0;
return 0;
}
.
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
11 Jan 2008 02:33:19 PM |
|
|
none <mikem891@hotmail.com> wrote:
I'm sure there's a better way to do this, I would really like
to be able to declare only one operator overload for each
operators. By the way I think I'm may be missing some important
points about templates, I find the class and typename keywords
really confusion. Should I use typename for member types and
class only for objects? (there's no informations about
templates in any of my C++ books)
for example:
template <class COMPLEXTYPE, typename TYPENAME>
class Ccomplex
In that context, there is no difference between the keyword "class" and
the keyword "typename". I use typename because it is linguistically more
accurate.
.
|
|
|
| User: "red floyd" |
|
| Title: Re: operator overloading question |
11 Jan 2008 03:36:32 PM |
|
|
Daniel T. wrote:
none <mikem891@hotmail.com> wrote:
for example:
template <class COMPLEXTYPE, typename TYPENAME>
class Ccomplex
In that context, there is no difference between the keyword "class" and
the keyword "typename". I use typename because it is linguistically more
accurate.
In this context, I like to use class vs. typename as a
self-documentation aid. Since in this context, there's no difference in
a template parameter list, I use "class" as a reminder that the template
is intended to be instantiated with a class type, and "typename" when
any type at all may be used.
Note: this is a personal preference as to coding style, and not to be
treated as gospel.
.
|
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
11 Jan 2008 03:38:35 PM |
|
|
none <mikem891@hotmail.com> wrote:
As a style issue. I find it odd that you abbreviate imaginary to img and
yet use COMPLEXTYPE which is usually abbreviated to just T, Ty, or Type.
Also, using two Cs in the name of the class is confusing, just call it
"Complex" not "Ccomplex".
Lastly, at least look at the official interface for the standard complex
class:
http://www.dinkumware.com/manuals/default.aspx?manual=compleat&page=compl
ex.html#complex
template <class COMPLEXTYPE>
class Ccomplex
{
public:
COMPLEXTYPE real;
COMPLEXTYPE img;
// Constructor
Redundant comment, of course the member-function is a constructor.
Ccomplex (void)
Remove "void" above. It is unnecessary.
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4
{
real = 0;
img = 0;
}
Get in the habit of using initializer lists instead of assignment in the
body of the constructor.
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
// Constructor (conversion operator)
Redundant comment...
Ccomplex (COMPLEXTYPE val)
{
real = val;
img = 0;
}
What about a two argument constructor? That would make sense...
// Destructor
~Ccomplex (void) { }
The compiler provides a destructor that does exactly what yours does. So
remove yours and let the compiler do its job.
inline Ccomplex<COMPLEXTYPE> operator = (Ccomplex<COMPLEXTYPE>
source);
The compiler provides an op= that does exactly what yours does. So
remove yours and let the compiler do its job.
friend inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE>
source);
friend inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, const COMPLEXTYPE
source);
friend inline Ccomplex<COMPLEXTYPE> operator + \
(const COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE>
source);
The backslash isn't necessary in the above (or anywhere else if this
program for that matter. Don't complicate your life by using unnecessary
constructs.
"friend" isn't necessary. There is nothing private/protected in the
class.
"inline" isn't necessary. The compiler will inline where it can.
The three declarations above aren't necessary.
protected:
Spurious access specifier.
};
typedef Ccomplex<double> COMPLEX;
Reserve all upper-case declarations for macros.
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> \
Ccomplex<COMPLEXTYPE>::operator = (Ccomplex<COMPLEXTYPE>
source)
{
this->real = source.real;
this->img = source.img;
return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> source)
'dest' and 'source' are misleading names for the arguments.
{
Ccomplex<COMPLEXTYPE> newdest;
newdest.real = dest.real + source.real;
newdest.img = dest.img + source.img;
return newdest;
If you make a two argument constructor, the above can be made much
simpler...
return Complex<T>( left.real + right.real, left.img + right.img );
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(Ccomplex<COMPLEXTYPE> dest,const COMPLEXTYPE source)
{ return dest + (Ccomplex<COMPLEXTYPE>) source; }
Better to call the conversion constructor explicitly.
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
(const COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE> source)
{ return (Ccomplex<COMPLEXTYPE>) dest + source; }
const is unnecessary as used above. Generally, declaring arguments
"const type" is considered bad form. Pass the arguments either by value,
or by const reference.
int main(int argc, char* argv[])
If you don't use the arguments, don't put them in.
{
COMPLEX complex;
COMPLEX complex2;
complex.real = 5;
complex.img = 2;
// WORKS
complex2 = (COMPLEX) 1 + complex;
complex = complex2 + (COMPLEX) 2;
Prefer calling the conversion constructor explicitly instead of using a
cast.
// WORKS but must use double notation to avoid
// template ambiguities between in and double
complex2 = 1.0 + complex;
complex = complex2 + 2.0;
return 0;
"return 0;" from main is unnecessary.
}
So, if you accept all my changes, you end up with:
template <class T>
class Complex
{
public:
T real;
T img;
Complex(): real(0), img(0) { }
Complex(T val): real(val), img(0) { }
Complex(T real, T img): real(real), img(img) { }
};
template <typename T>
Complex<T> operator +(Complex<T> left, Complex<T> right)
{
return Complex<T>(left.real + right.real, left.img + right.img);
}
template <typename T>
Complex<T> operator +(Complex<T> left, T right)
{
return Complex<T>(left.real + right, left.img);
}
template <typename T>
Complex<T> operator +(const T left, Complex<T> right)
{
return Complex<T>(left + right.real, right.img);
}
typedef Complex<double> ComplexDouble;
int main(int argc, char* argv[])
{
ComplexDouble complex;
ComplexDouble complex2;
complex.real = 5;
complex.img = 2;
complex2 = ComplexDouble(1) + complex;
complex = complex2 + ComplexDouble(2);
complex2 = 1.0 + complex;
complex = complex2 + 2.0;
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
11 Jan 2008 07:19:12 PM |
|
|
On Jan 11, 4:38=A0pm, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
As a style issue. I find it odd that you abbreviate imaginary to img and
yet use COMPLEXTYPE which is usually abbreviated to just T, Ty, or Type.
Also, using two Cs in the name of the class is confusing, just call it
"Complex" not "Ccomplex".
Lastly, at least look at the official interface for the standard complex
class:
http://www.dinkumware.com/manuals/default.aspx?manual=3Dcompleat&page=3Dc.=
...
ex.html#complex
template <class COMPLEXTYPE>
class Ccomplex
{
=A0public:
=A0 =A0COMPLEXTYPE real;
=A0 =A0COMPLEXTYPE img;
=A0 =A0// Constructor
Redundant comment, of course the member-function is a constructor.
=A0 =A0Ccomplex (void)
Remove "void" above. It is unnecessary.http://www.parashift.com/c++-faq-li=
te/newbie.html#faq-29.4
=A0 =A0{
=A0 =A0 real =3D 0;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
Get in the habit of using initializer lists instead of assignment in the
body of the constructor.http://www.parashift.com/c++-faq-lite/ctors.html#f=
aq-10.6
=A0 =A0// Constructor (conversion operator)
Redundant comment...
=A0 =A0Ccomplex (COMPLEXTYPE val)
=A0 =A0{
=A0 =A0 real =3D val;
=A0 =A0 img =A0=3D 0;
=A0 =A0}
What about a two argument constructor? That would make sense...
=A0 =A0// Destructor
=A0 =A0~Ccomplex (void) { }
The compiler provides a destructor that does exactly what yours does. So
remove yours and let the compiler do its job.
=A0 =A0inline Ccomplex<COMPLEXTYPE> operator =3D (Ccomplex<COMPLEXTYPE>
source);
The compiler provides an op=3D that does exactly what yours does. So
remove yours and let the compiler do its job.
=A0 =A0friend inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest, Ccomplex<CO=
MPLEXTYPE>
source);
=A0 =A0friend inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest, const COMPL=
EXTYPE
source);
=A0 =A0friend inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (const COMPLEXTYPE dest, Ccomplex<COMPLE=
XTYPE>
source);
The backslash isn't necessary in the above (or anywhere else if this
program for that matter. Don't complicate your life by using unnecessary
constructs.
"friend" isn't necessary. There is nothing private/protected in the
class.
"inline" isn't necessary. The compiler will inline where it can.
The three declarations above aren't necessary.
=A0protected:
Spurious access specifier.
};
typedef Ccomplex<double> COMPLEX;
Reserve all upper-case declarations for macros.
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> \
=A0 =A0 =A0 =A0Ccomplex<COMPLEXTYPE>::operator =3D (Ccomplex<COMPLEXTYPE=
source)
{
=A0 this->real =3D source.real;
=A0 this->img =A0=3D source.img;
=A0 return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest, Ccomplex<COMPLEXTYPE> source)
'dest' and 'source' are misleading names for the arguments.
{
=A0 Ccomplex<COMPLEXTYPE> newdest;
=A0 newdest.real =3D dest.real + source.real;
=A0 newdest.img =A0=3D dest.img =A0+ source.img;
=A0 return newdest;
If you make a two argument constructor, the above can be made much
simpler...
=A0 =A0return Complex<T>( left.real + right.real, left.img + right.img );
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 (Ccomplex<COMPLEXTYPE> dest,const COMPLEXTYPE source)
{ return dest + (Ccomplex<COMPLEXTYPE>) source; }
Better to call the conversion constructor explicitly.
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + \
=A0 =A0 =A0 (const COMPLEXTYPE dest, Ccomplex<COMPLEXTYPE> source)
{ return (Ccomplex<COMPLEXTYPE>) dest + source; }
const is unnecessary as used above. Generally, declaring arguments
"const type" is considered bad form. Pass the arguments either by value,
or by const reference.
int main(int argc, char* argv[])
If you don't use the arguments, don't put them in.
{
=A0 =A0 COMPLEX complex;
=A0 =A0 COMPLEX complex2;
=A0 =A0 complex.real =3D 5;
=A0 =A0 complex.img =A0=3D 2;
=A0 =A0 // WORKS
=A0 =A0 complex2 =3D (COMPLEX) 1 + complex;
=A0 =A0 complex =A0=3D complex2 + (COMPLEX) 2;
Prefer calling the conversion constructor explicitly instead of using a
cast.
=A0 =A0 // WORKS but must use double notation to avoid
=A0 =A0 // template ambiguities between in and double
=A0 =A0 complex2 =3D 1.0 + complex;
=A0 =A0 complex =A0=3D complex2 + 2.0;
=A0 =A0 return 0;
"return 0;" from main is unnecessary.
}
So, if you accept all my changes, you end up with:
=A0 =A0template <class T>
class Complex
{
public:
=A0 =A0T real;
=A0 =A0T img;
=A0 =A0Complex(): real(0), img(0) { }
=A0 =A0Complex(T val): real(val), img(0) { }
=A0 =A0Complex(T real, T img): real(real), img(img) { }
};
=A0 =A0template <typename T>
Complex<T> operator +(Complex<T> left, Complex<T> right)
{
=A0 =A0return Complex<T>(left.real + right.real, left.img + right.img);
}
=A0 =A0template <typename T>
Complex<T> operator +(Complex<T> left, T right)
{
=A0 =A0return Complex<T>(left.real + right, left.img);
}
=A0 =A0template <typename T>
Complex<T> operator +(const T left, Complex<T> right)
{
=A0 =A0return Complex<T>(left + right.real, right.img);
}
typedef Complex<double> ComplexDouble;
int main(int argc, char* argv[])
{
=A0 =A0ComplexDouble complex;
=A0 =A0ComplexDouble complex2;
=A0 =A0complex.real =3D 5;
=A0 =A0complex.img =A0=3D 2;
=A0 =A0complex2 =3D ComplexDouble(1) + complex;
=A0 =A0complex =A0=3D complex2 + ComplexDouble(2);
=A0 =A0complex2 =3D 1.0 + complex;
=A0 =A0complex =A0=3D complex2 + 2.0;
}- Hide quoted text -
- Show quoted text -
thanks a lot for your help!
.
|
|
|
|
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
10 Jan 2008 08:35:25 AM |
|
|
In article
<96259a9a-f6aa-4202-91ad-ebe08ec2ee8d@1g2000hsl.googlegroups.com>,
none <mikem891@hotmail.com> wrote:
I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:
intvariable = fooclass;
here's a pseudo code of what I'm doing:
template <class COMPLEXTYPE>
class foo
{
public:
COMPLEXTYPE data;
foo (void) { }
~foo (void) { }
foo<COMPLEXTYPE> operator = (foo<COMPLEXTYPE> source);
unexpected ';'
{
this->data = source.data;
return *this;
}
foo<COMPLEXTYPE> operator = (COMPLEXTYPE source);
unexpected ';'
{
this->data = source;
return *this;
}
}
missing ';'
// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>
missing "typename"
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE> source)
{
dest = (COMPLEXTYPE) source.data;
return dest;
}
operator= must be a non-static member-function
There are a couple of ways you can do this. One:
template < typename T >
T foo<T>::getData() const {
return source.data;
}
or you could:
template < typename T >
T& assign( T& dest, const foo<T>& source ) {
dest = source.data;
return dest;
}
main (void)
declaration of "main" with no type is forbidden.
{
foo<int> FOOINT;
foo<int> FOOINT2;
int integer;
FOOTINT.data = 1;
FOOTINT was not declared.
integer = 4;
FOOINT2 = FOOINT; // works
FOOINT = integer; // works
integer = FOOINT2; // does not work
Depending on what you did above, either:
integer = FOOINT2.getData();
or
assign( integer, FOOINT2 );
}
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
10 Jan 2008 09:58:50 AM |
|
|
Sorry for the errors in the code, that's why
I said pseudo code, this is not the code I'm
actually using. the one I'm using do not contain
the stupid ; and main errors.
I know I could simply do this
int integer = foo.data;
but I want to do it for a complex number class and I would
really like to be able to do this:
int real = complex;
instead of
int real = complex.real;
the error I get is that the compiler do not let me overload
the = operator for
interger = foo;'
it tells me that the operator = do not belong to any class.
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
10 Jan 2008 10:10:32 AM |
|
|
for example I tried this:
template <typename TYPE>
TYPE operator TYPE = (TYPE *type1, foo<TYPE> type2)
{
*type1 = (TYPE) type2.data;
};
and I get these errors:
user-defined conversion cannot specify a return type
incorrect user-defined conversion syntax: illegal indirections
I also tried:
template <typename TYPE>
TYPE operator TYPE = (foo<TYPE> type2)
{
return (TYPE) type2.data;
};
or
template <typename TYPE>
TYPE operator TYPE = (TYPE type1, foo<TYPE> type2)
{
type1 = (TYPE) type2.data;
return type1;
};
and
template <typename TYPE>
operator TYPE = (TYPE type1, foo<TYPE> type2)
{
type1 = (TYPE) type2.data;
};
none of them works.
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
10 Jan 2008 10:33:40 AM |
|
|
I would also like to be able to do
foo fooobj,fooobj2;
fooobj = integer + fooobj2;
but I can't find a way to do this, the only overloads I
was able to make is:
foo fooobj,fooobj2;
fooobj = fooobk + integer;
and
foo fooobj,fooobj2;
fooobj = fooobj + fooobj2;
.
|
|
|
| User: "Joe Greer" |
|
| Title: Re: operator overloading question |
10 Jan 2008 11:44:34 AM |
|
|
none <mikem891@hotmail.com> wrote in news:eecdc33b-dbfe-4f93-b2af-
e4dee0475b6e@c4g2000hsg.googlegroups.com:
I would also like to be able to do
foo fooobj,fooobj2;
fooobj = integer + fooobj2;
but I can't find a way to do this, the only overloads I
was able to make is:
foo fooobj,fooobj2;
fooobj = fooobk + integer;
and
foo fooobj,fooobj2;
fooobj = fooobj + fooobj2;
You can do that by declaring your binary operators outside your class.
In the 'a picture is worth a thousand words category', here is a small
sample program.
#include <iostream>
#include <ostream>
class foo
{
public:
foo(int a) : m_a(a) {}
foo & operator+=(foo const & a)
{
m_a += a.m_a;
return *this;
}
void print(std::ostream & o) const
{
o << m_a;
}
private:
int m_a;
};
std::ostream & operator<<(std::ostream & o, foo const & f)
{
f.print(o);
return o;
}
foo operator+(int a, foo const & f)
{
foo aFoo(a); // convert the int to a foo
aFoo += f;
return aFoo;
}
foo operator+(foo const & f, int a)
{
foo aFoo(a); // convert the int to a foo
aFoo += f;
return aFoo;
}
int main()
{
foo a(2);
foo b(0);
b = a + 4;
std::cout << "b = " << b << std::endl;
b = 4 + a;
std::cout << "b = " << b << std::endl;
}
Hope that helps,
joe
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
10 Jan 2008 11:56:43 AM |
|
|
On Jan 10, 12:44=A0pm, Joe Greer <jgr...@doubletake.com> wrote:
none <mikem...@hotmail.com> wrote in news:eecdc33b-dbfe-4f93-b2af-
e4dee0475...@c4g2000hsg.googlegroups.com:
I would also like to be able to do
foo fooobj,fooobj2;
fooobj =3D integer + fooobj2;
but I can't find a way to do this, the only overloads I
was able to make is:
foo fooobj,fooobj2;
fooobj =3D fooobk + integer;
and
foo fooobj,fooobj2;
fooobj =3D fooobj + fooobj2;
You can do that by declaring your binary operators outside your class. =A0=
In the 'a picture is worth a thousand words category', here is a small
sample program.
#include <iostream>
#include <ostream>
class foo
{
public:
=A0 =A0foo(int a) : m_a(a) {}
=A0 =A0foo & operator+=3D(foo const & a)
=A0 =A0{
=A0 =A0 =A0 m_a +=3D a.m_a;
=A0 =A0 =A0 return *this;
=A0 =A0}
=A0 =A0void print(std::ostream & o) const
=A0 =A0{
=A0 =A0 =A0 o << m_a;
=A0 =A0}
private:
=A0 =A0int m_a;
};
std::ostream & operator<<(std::ostream & o, foo const & f)
{
=A0 =A0f.print(o);
=A0 =A0return o;
}
foo operator+(int a, foo const & f)
{
=A0 =A0foo aFoo(a); // convert the int to a foo
=A0 =A0aFoo +=3D f;
=A0 =A0return aFoo;
}
foo operator+(foo const & f, int a)
{
=A0 =A0foo aFoo(a); // convert the int to a foo
=A0 =A0aFoo +=3D f;
=A0 =A0return aFoo;
}
int main()
{
=A0 =A0foo a(2);
=A0 =A0foo b(0);
=A0 =A0b =3D a + 4;
=A0 =A0std::cout << "b =3D " << b << std::endl;
=A0 =A0b =3D 4 + a;
=A0 =A0std::cout << "b =3D " << b << std::endl;
}
Hope that helps,
joe
thanks a lot, yes it will help. I will try it tonight. will
it work with templates? because I think I tried something
similar to this yesterday without any success. my binary
operators are already declared outside the class.
.
|
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
10 Jan 2008 01:58:52 PM |
|
|
none <mikem891@hotmail.com> wrote:
I would also like to be able to do
foo fooobj,fooobj2;
fooobj = integer + fooobj2;
but I can't find a way to do this, the only overloads I
was able to make is:
foo fooobj,fooobj2;
fooobj = fooobk + integer;
and
foo fooobj,fooobj2;
fooobj = fooobj + fooobj2;
Why invent a Complex class where there is already a well designed one in
the language. Just use std::complex.
.
|
|
|
| User: "none" |
|
| Title: Re: operator overloading question |
10 Jan 2008 04:35:31 PM |
|
|
On Jan 10, 2:58=A0pm, "Daniel T." <danie...@earthlink.net> wrote:
none <mikem...@hotmail.com> wrote:
I would also like to be able to do
foo fooobj,fooobj2;
fooobj =3D integer + fooobj2;
but I can't find a way to do this, the only overloads I
was able to make is:
foo fooobj,fooobj2;
fooobj =3D fooobk + integer;
and
foo fooobj,fooobj2;
fooobj =3D fooobj + fooobj2;
Why invent a Complex class where there is already a well designed one in
the language. Just use std::complex.- Hide quoted text -
- Show quoted text -
for fun, and because I use visual c++ and I thought that it
do not include the complex class, but maybe I'm wrong.
.
|
|
|
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
10 Jan 2008 11:16:52 AM |
|
|
none <mikem891@hotmail.com> wrote:
Sorry for the errors in the code, that's why
I said pseudo code, this is not the code I'm
actually using. the one I'm using do not contain
the stupid ; and main errors.
I know I could simply do this
int integer = foo.data;
but I want to do it for a complex number class and I would
really like to be able to do this:
int real = complex;
That's not a smart thing to do. What if I want the imaginary part?
int imaginary = complex; // how can the compiler tell?
the error I get is that the compiler do not let me overload
the = operator for
interger = foo;'
it tells me that the operator = do not belong to any class.
operator= must be a non-static member-function. [full stop]
.
|
|
|
|
|
|
| User: "Pete Becker" |
|
| Title: Re: operator overloading question |
10 Jan 2008 07:29:26 AM |
|
|
On 2008-01-10 07:47:22 -0500, none <mikem891@hotmail.com> said:
// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE> source)
When you're lost, simplify. Try this with a class rather than a
template. Then read about user-defined assignment operators. Any basic
text will tell you what's going on.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
.
|
|
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: operator overloading question |
10 Jan 2008 12:07:26 PM |
|
|
On 2008-01-10 13:47, none wrote:
I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:
intvariable = fooclass;
You can do that with a conversion operator:
#include <iostream>
class Foo
{
int m_i;
public:
Foo(int i) : m_i(i) {}
operator int() { return m_i; }
};
int main()
{
Foo f(4);
int i = f;
std::cout << i;
}
--
Erik Wikström
.
|
|
|
| User: "Daniel T." |
|
| Title: Re: operator overloading question |
10 Jan 2008 01:57:37 PM |
|
|
Erik Wikström <Erik-wikstrom@telia.com> wrote:
On 2008-01-10 13:47, none wrote:
I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:
intvariable = fooclass;
You can do that with a conversion operator:
#include <iostream>
class Foo
{
int m_i;
public:
Foo(int i) : m_i(i) {}
operator int() { return m_i; }
};
int main()
{
Foo f(4);
int i = f;
std::cout << i;
}
That's the unsafe choice. Better would be to make the constructor
explicit and provide a named conversion operator. That way you can tell
exactly what is going on at the call point and no mysterious temporaries
are created.
.
|
|
| | |