|
From: Marc H. <ma...@e-...> - 2003-07-28 17:14:04
|
Hi All,
I am developing a class for use as a type. So let's imagine the following
scenario (this example is contrived and not useful in any way, but it
shorter than the code for my class):
class CNumber
{
public:
CNumber(int n = 0) : m_n(n) {};
virtual ~CNumber() {};
unsigned char operator()()
{
return (unsigned char) m_n;
};
long operator()()
{
return (long) m_n;
};
private:
int m_n;
};
Compiling this tells me that the () operators cannot be overloaded! If I
remove one of them, then it compiles just fine. Are you going to tell me
that I can only overload the () operator once?
__________________________________
Marc Heiligers
|
|
From: Abhijit S. <mu...@gm...> - 2003-07-28 18:13:29
|
> unsigned char operator()()
> {
> return (unsigned char) m_n;
> };
> long operator()()
> {
> return (long) m_n;
> };
> };
>
> Compiling this tells me that the () operators cannot be overloaded! If I
> remove one of them, then it compiles just fine. Are you going to tell me
> that I can only overload the () operator once?
Overloading is having a different parameter list, not different return
values. Here, you have the same parameter list, so the compiler generates
the same signature. That's why the error.
You should have only one operator, which returns the actual type of m_n. It
is up to the user of the class to cast it. This way, you ensure that the
end-user knows that he's casting, and knows that he can get into trouble
that way.
--
Abhijit Shylanath
Homepage: http://mudeth.tripod.com/
|
|
From: Ioannis V. <iv...@em...> - 2003-07-28 20:16:30
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Marc Heiligers > Sent: Monday, July 28, 2003 8:13 PM > To: dev...@li... > Subject: [Dev-C++] Overloading ()() > > unsigned char operator()() operator unsigned char() The return value is the type. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Abhijit S. <mu...@gm...> - 2003-07-29 17:41:06
|
> operator unsigned char() > > The return value is the type. Okay, so my post (argument list should be different, not return type) isn't valid, I guess. Sorry. -- Abhijit Shylanath Homepage: http://mudeth.tripod.com/ |
|
From: Per W. <pw...@ia...> - 2003-07-29 18:46:26
|
It is valid in the sense that a class shouldn't implement a lot of type
conversions just because it can. It should still be up to the user to
perorm any typecasts from int to xx.
Normally, operator type() is used to perform type-conversions that may not
be performed by a single type-cast - converting an integer to a string, or
an object that encapsulates several sub-objects, for example:
class CSurfInfo;
class CVec3;
class CTransform;
class C3DObject {
public:
operator CSurfInfo&();
operator CTransform&();
operator CVec3&();
};
C3DObject *pObj;
...
setSurface((CSurfInfo*)*pObj);
/Per W
On Tue, 29 Jul 2003, Abhijit Shylanath wrote:
> > operator unsigned char()
> >
> > The return value is the type.
>
> Okay, so my post (argument list should be different, not return type) isn't
> valid, I guess. Sorry.
>
> --
> Abhijit Shylanath
> Homepage: http://mudeth.tripod.com/
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Marc H. <ma...@e-...> - 2003-07-30 05:07:36
|
Hi All,
As I said, the example was contrived and not useful. Casting to different
int types is not what I had in mind, but it served to get the question
across without declaring 10 different classes and overloading the type
operator. Anyway thanks for the input, everybody. Ioannis' answer was
correct, although it was so brief I had to read it ten times before I
understood what he meant.
__________________________________
Marc Heiligers
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Per
Westermark
Sent: 29 July 2003 20:26
To: Abhijit Shylanath
Cc: Ioannis Vranos; dev...@li...
Subject: RE: [Dev-C++] Overloading ()()
It is valid in the sense that a class shouldn't implement a lot of type
conversions just because it can. It should still be up to the user to
perorm any typecasts from int to xx.
Normally, operator type() is used to perform type-conversions that may not
be performed by a single type-cast - converting an integer to a string, or
an object that encapsulates several sub-objects, for example:
class CSurfInfo;
class CVec3;
class CTransform;
class C3DObject {
public:
operator CSurfInfo&();
operator CTransform&();
operator CVec3&();
};
C3DObject *pObj;
...
setSurface((CSurfInfo*)*pObj);
/Per W
On Tue, 29 Jul 2003, Abhijit Shylanath wrote:
> > operator unsigned char()
> >
> > The return value is the type.
>
> Okay, so my post (argument list should be different, not return type)
isn't
> valid, I guess. Sorry.
>
> --
> Abhijit Shylanath
> Homepage: http://mudeth.tripod.com/
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <iv...@em...> - 2003-07-30 08:06:57
|
> -----Original Message----- > From: Marc Heiligers [mailto:ma...@e-...]=20 > Sent: Wednesday, July 30, 2003 8:07 AM > To: 'Per Westermark'; 'Abhijit Shylanath' > Cc: 'Ioannis Vranos'; dev...@li... > Subject: RE: [Dev-C++] Overloading ()() >=20 >=20 > Hi All, >=20 > As I said, the example was contrived and not useful. Casting=20 > to different > int types is not what I had in mind, but it served to get the question > across without declaring 10 different classes and overloading the type > operator. Anyway thanks for the input, everybody. Ioannis' answer was > correct, although it was so brief I had to read it ten times before I > understood what he meant. I have been very busy these last days that essentially I haven't read = any emails on this thread since my answer. I saw that type casts were = mentioned in the mean time. What casts?! These "type conversions" are not to be used regularily, but only in = specific cases when doing otherwise introduces significant disadvantages. Use encapsulation and abstraction for the usual stuff (access via member functions and stuff). You will find that in most cases that the implicit type conversions you = need will occur via the implicit calls of constructors and the creation of temporaries (which sometimes we wish to avoid thus the use of the = keyword explicit, but that's another story). Ioannis Vranos =20 * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Graham D. <g.d...@nt...> - 2003-07-29 20:15:16
|
Actually, I thought your first answer was absolutely correct. Hardly anything in the original post suggested that tho OP was trying to implement conversion operators. The OP asked about overloading operator() and the sample code showed an attempt to overload operator(), since there were two sets of parentheses shown, and the word operator came after the type. When I read it, I assumed that this was simply an inappropriate use of operator() to do some type conversion. I.e. I though that the intent was to use it as: CNumber n; char c = n(); // attempt to call char CNumber::operator()(); long l = n(); // attempt to call long CNumber::operator()(); Doomed to failure, of course, because, as you pointed out, you can't overload on return type and also because using operator() in this way is wholly inappropriate. Abhijit Shylanath wrote: >>operator unsigned char() >> >>The return value is the type. >> >> > >Okay, so my post (argument list should be different, not return type) isn't >valid, I guess. Sorry. > >-- >Abhijit Shylanath >Homepage: http://mudeth.tripod.com/ > > > >------------------------------------------------------- >This SF.Net email sponsored by: Free pre-built ASP.NET sites including >Data Reports, E-commerce, Portals, and Forums are available now. >Download today and enter to win an XBOX or Visual Studio .NET. >http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > |