Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > user defined conversion operator or operator overloading?

Reply
Thread Tools

user defined conversion operator or operator overloading?

 
 
hurcan solter
Guest
Posts: n/a
 
      08-29-2007
I have an host class that holds fundamental types

template<typename T>
struct Generic{

Generic(T val= T()):mval(val){}
operator T(){return mval
T mval;
}

template<typename T1,typename T2>
Generic<T1 or T2 ???>operator+(const Generic<T1>& lhs,const
Generic<T2>& rhs)
{
return Generic<T1 or T2 ???>(lhs.mval+rhs.mval);
}

Is there a compelling reason to discard the conversion operator and
define overloaded arithmetic operators because they are considered
dangerous?I'd like that class to behave like fundamental types.but i
dont want to overload every operator over there . what are the issues
i should be aware of if i stick with the user defined conversion
operator?

thanks a lot
hurcan....

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-29-2007
hurcan solter wrote:
> I have an host class that holds fundamental types
>
> template<typename T>
> struct Generic{
>
> Generic(T val= T()):mval(val){}
> operator T(){return mval
> T mval;
> }
>
> template<typename T1,typename T2>
> Generic<T1 or T2 ???>operator+(const Generic<T1>& lhs,const
> Generic<T2>& rhs)
> {
> return Generic<T1 or T2 ???>(lhs.mval+rhs.mval);



The presense of the question marks seems to indicate that you do
not know what to use here. You essentially need a helper class
that would define the type of the addition of T1 and T2. You could
hard-code those, or see if Boost folks have already come up with
something.

> }
>
> Is there a compelling reason to discard the conversion operator and
> define overloaded arithmetic operators because they are considered
> dangerous?


Cosidered dangerous by whom?

>I'd like that class to behave like fundamental types.but i
> dont want to overload every operator over there . what are the issues
> i should be aware of if i stick with the user defined conversion
> operator?


To be honest with your, I am not even sure why you'd need such
a type like your 'Generic'. What purpose would is serve?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
hurcan solter
Guest
Posts: n/a
 
      08-29-2007

> The presense of the question marks seems to indicate that you do
> not know what to use here. You essentially need a helper class
> that would define the type of the addition of T1 and T2. You could
> hard-code those, or see if Boost folks have already come up with
> something.
>

it means I am unable to determine whether T1 can be promoted to T2
or vice versa
so i can return the correct Generic<T> without loss of
information.if they were fundamental types compiler would handle it
for me...

> Cosidered dangerous by whom?


same people who invented std::string.c_str() ? Or (a long shot but)
the need for explicit keyword.
> >I'd like that class to behave like fundamental types.but i
> > dont want to overload every operator over there . what are the issues
> > i should be aware of if i stick with the user defined conversion
> > operator?

>
> To be honest with your, I am not even sure why you'd need such
> a type like your 'Generic'. What purpose would is serve?


it was for demonstrating the gist of the problem(the real name isn't
even Generic), In reality it has some policies can be configured into
it and some other methods.

hurcan ...


 
Reply With Quote
 
Cholo Lennon
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 3:31 pm, hurcan solter <(E-Mail Removed)> wrote:
> > The presense of the question marks seems to indicate that you do
> > not know what to use here. You essentially need a helper class
> > that would define the type of the addition of T1 and T2. You could
> > hard-code those, or see if Boost folks have already come up with
> > something.

>
> it means I am unable to determine whether T1 can be promoted to T2
> or vice versa
> so i can return the correct Generic<T> without loss of
> information.if they were fundamental types compiler would handle it
> for me...
>
> > Cosidered dangerous by whom?

>
> same people who invented std::string.c_str() ? Or (a long shot but)
> the need for explicit keyword.
>
> > >I'd like that class to behave like fundamental types.but i
> > > dont want to overload every operator over there . what are the issues
> > > i should be aware of if i stick with the user defined conversion
> > > operator?

>
> > To be honest with your, I am not even sure why you'd need such
> > a type like your 'Generic'. What purpose would is serve?

>
> it was for demonstrating the gist of the problem(the real name isn't
> even Generic), In reality it has some policies can be configured into
> it and some other methods.
>
> hurcan ...


Take a look to boost::is_convertible for a possible implementation

http://www.boost.org/doc/html/boost_...is_convertible


Regards

--
Cholo Lennon
Bs.As.
ARG


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
'no user-defined conversion exists' error Anna C++ 1 01-09-2007 05:31 AM
conversion from one user defined type to another Kavya C++ 2 10-31-2006 03:50 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Templates and user defined conversion operator uvts_cvs@yahoo.com C++ 0 03-23-2005 10:24 AM



Advertisments