473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how do I overload (type) operator?

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?


Jul 22 '05 #1
3 1168

"sean worlock" <se**********@sworlock.freeserve.co.uk> wrote in message
news:cl**********@news8.svr.pol.co.uk...
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
Jul 22 '05 #2
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
Jul 22 '05 #3
On Thu, 28 Oct 2004 09:51:48 +0300, "Catalin Pitis"
<ca***********@iquestint.com.renameme> wrote:

"sean worlock" <se**********@sworlock.freeserve.co.uk> wrote in message
news:cl**********@news8.svr.pol.co.uk...
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
6
by: Harry J. Smith | last post by:
How do you overload the = operator? public static MultiUI operator = (MultiUI left, MultiUI right) { return right; }
7
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.