Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how do I overload (type) operator?

Reply
Thread Tools

how do I overload (type) operator?

 
 
sean worlock
Guest
Posts: n/a
 
      10-28-2004
I have written a simple class that modals the action of a complex number.
This class is called CComplex.
I wish to make my class as versatile as possable.
Thus lets say I have some variables defined as following:

int value1;
double value2;
float value3;

and three Complex objects defined as:

CComplex ONE,TWO,THREE;

I wish to perform the following...

ONE=(CComplex) value1;
TWO=(CComplex) value2;
THREE=(CComplex) value3;

thus I need to create a type operator for my CComplex class.
I have read many texts for the conversion of my class into other types but
not the other way.

could anybody point me in the correct direction for the prototype and
function syntax to complete this?




 
Reply With Quote
 
 
 
 
Catalin Pitis
Guest
Posts: n/a
 
      10-28-2004

"sean worlock" <(E-Mail Removed)> wrote in message
news:clq4mr$rvm$(E-Mail Removed)...
>I have written a simple class that modals the action of a complex number.
> This class is called CComplex.
> I wish to make my class as versatile as possable.
> Thus lets say I have some variables defined as following:
>
> int value1;
> double value2;
> float value3;
>
> and three Complex objects defined as:
>
> CComplex ONE,TWO,THREE;
>
> I wish to perform the following...
>
> ONE=(CComplex) value1;
> TWO=(CComplex) value2;
> THREE=(CComplex) value3;
>
> thus I need to create a type operator for my CComplex class.
> I have read many texts for the conversion of my class into other types but
> not the other way.
>
> could anybody point me in the correct direction for the prototype and
> function syntax to complete this?
>


Just define constructor:

CComplex::CComplex( double value).

The statements can be then rewritten like:

ONE=value1;
TWO=value2;
THREE=value3;

Catalin


 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      10-28-2004
sean worlock wrote:

> I have written a simple class that modals the action of a complex number.
> This class is called CComplex.
> I wish to make my class as versatile as possable.
> Thus lets say I have some variables defined as following:
>
> int value1;
> double value2;
> float value3;
>
> and three Complex objects defined as:
>
> CComplex ONE,TWO,THREE;
>
> I wish to perform the following...
>
> ONE=(CComplex) value1;
> TWO=(CComplex) value2;
> THREE=(CComplex) value3;
>
> thus I need to create a type operator for my CComplex class.
> I have read many texts for the conversion of my class into other types but
> not the other way.
>
> could anybody point me in the correct direction for the prototype and
> function syntax to complete this?


You can template the the hole class:

template<typename UnderLyingType>
class CComplex

Mayby you'd have to check if the type is a correct one, such as double
and so on.

HTH && Kind regards,
Nicolas
 
Reply With Quote
 
Tom Widmer
Guest
Posts: n/a
 
      10-28-2004
On Thu, 28 Oct 2004 09:51:48 +0300, "Catalin Pitis"
<(E-Mail Removed)> wrote:

>
>"sean worlock" <(E-Mail Removed)> wrote in message
>news:clq4mr$rvm$(E-Mail Removed)...
>>I have written a simple class that modals the action of a complex number.
>> This class is called CComplex.
>> I wish to make my class as versatile as possable.
>> Thus lets say I have some variables defined as following:
>>
>> int value1;
>> double value2;
>> float value3;
>>
>> and three Complex objects defined as:
>>
>> CComplex ONE,TWO,THREE;
>>
>> I wish to perform the following...
>>
>> ONE=(CComplex) value1;
>> TWO=(CComplex) value2;
>> THREE=(CComplex) value3;
>>
>> thus I need to create a type operator for my CComplex class.
>> I have read many texts for the conversion of my class into other types but
>> not the other way.
>>
>> could anybody point me in the correct direction for the prototype and
>> function syntax to complete this?
>>

>
>Just define constructor:
>
>CComplex::CComplex( double value).
>
>The statements can be then rewritten like:
>
>ONE=value1;
>TWO=value2;
>THREE=value3;


Note that if you (the OP) want to require the cast notation, you can
declare the constructor "explicit". e.g.

explicit CComplex(double value); //don't use explicit in definition

I can't think of a good reason why you would want to do this though.
Also note that there is a perfectly good complex number class that is
part of the standard library: std::complex<double>, declared in the
<complex> header.

Tom
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
UDP source ports using PAT (NAT overload) Greg Grimes Cisco 3 08-16-2004 10:26 PM
Using multiple outside interface with ip nat overload Emanuel Cisco 1 02-25-2004 08:10 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments