 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
sean worlock Guest
|
Posted: Thu Oct 28, 2004 2:10 pm Post subject: 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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Mang Guest
|
Posted: Fri Oct 29, 2004 2:13 am Post subject: Re: How Do I overload (TYPE) operator? |
|
|
"sean worlock" <sean_worlock (AT) sworlock (DOT) freeserve.co.uk> schrieb im
Newsbeitrag news:clom3l$746$1 (AT) newsg2 (DOT) svr.pol.co.uk...
| Quote: | 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.
|
You mean an implicit (that is, not 'explicit') constructor?
class CComplex
{
public:
CComplex();
CComplex(int SomeInt); // note: implicit conversion possible
};
Then you simply write
ONE = value1;
value1 will be used to convert the int into a CComplex, which will then be
assigned to ONE.
Another alternative is, of course, to overload operator= for various types.
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jakacki Guest
|
Posted: Fri Oct 29, 2004 2:16 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
| Quote: | 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;
|
Add constructors from desired types:
class CComplex {
....
CComplex(int i) ....
CComplex(double d) ....
CComplex(float f) ....
....
};
Moreover, your assignments would be better written as
constructions, not conversions:
ONE = CComplex(value1);
TWO = CComplex(value2);
THREE = CComplex(value3);
And if it is the first place you use the variables, you can
do even better by writting the above as declarations (that
way you avoid gratituous copy assignments):
CComplex ONE(value1);
CComplex TWO(value2);
CComplex THREE(value3);
If you don't want your newly defined ctors to take part in
automatic conversions (and automatic conversions are often
misleading to programmers), do this:
....
explicit CComplex(int) ....
^^^^^^^^
....
HTH
Grzegorz
PS: It is common style to use ALL_CAPS identifiers for
macros only.
--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sven Rosiers Guest
|
Posted: Fri Oct 29, 2004 2:32 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
sean worlock wrote:
| Quote: | 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.
|
Why not use the std::complex template from the standard library?
[SNIP]
| Quote: | CComplex ONE,TWO,THREE;
I wish to perform the following...
ONE=(CComplex) value1;
TWO=(CComplex) value2;
THREE=(CComplex) value3;
|
Don't use C-style casts in a C++ program. Use C++-style casts instead.
| Quote: | 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?
|
Conversion of an object of some other type into an object of your class
is achieved with a conversion constructor (that is, a constructor that
can be called with a single parameter that is not marked explicit), like so:
class my_class {
public:
my_class(int);
};
This allows the compiler to implicitly convert an int into an object of
my_class whereever it needs to.
Sven R.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Fri Oct 29, 2004 2:32 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
"sean worlock" <sean_worlock (AT) sworlock (DOT) freeserve.co.uk> skrev i en meddelelse
news:clom3l$746$1 (AT) newsg2 (DOT) svr.pol.co.uk...
| Quote: | I have written a simple class that modals the action of a complex
number.
This class is called CComplex.
|
You should use the standard complex class instead, but assuming this is just
an exercise.....
| Quote: | 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;
|
Avoid the C-like way of converting between different types. This is
practically never necessary in C++. Also do not use all capitals for your
variable names (this should be reserved for macros),
| Quote: |
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.
|
It is called the constructor.
class CComplex
{
......
CComplex(double) {... }
.....
this lets you write the above the assignments.
/Peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Fri Oct 29, 2004 2:33 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
Hi,
| Quote: | 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.
|
This is not a type operator, this is rather a constructor from
int, double or float objects. Thus:
class CComplex {
double re,im;
Complex(double v)
: re(v), im(0.0)
{ }
Complex(float v)
: re(v), im(0.0)
{ }
Complex(int v)
: re(v), im(0.0)
{ }
//
// more to go here...
};
should do it. Then you'd be able to write.
ONE = 1;
and
ONE = CComplex(1);
As a side remark, it is not wrong, but against all good traditions to
use all-upper case names for objects.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Samuel Klatchko Guest
|
Posted: Fri Oct 29, 2004 2:38 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
"sean worlock" <sean_worlock (AT) sworlock (DOT) freeserve.co.uk> wrote
| Quote: | 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 think of that operation in two ways. One is converting from an int
to a complex. The other is constructing a complex from an int.
Since you have no control over int, you can't create a cast for it.
But you can create new constructors for your complex type:
class CComplex
{
public:
CComplex(int);
CComplex(double);
...
samuel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Oct 29, 2004 2:39 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
"sean worlock" <sean_worlock (AT) sworlock (DOT) freeserve.co.uk> wrote
| Quote: | 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.
I wish to perform the following...
ONE=(CComplex) value1; // an integer
TWO=(CComplex) value2; // a double
THREE=(CComplex) value3; // a float
thus I need to create a type operator for my CComplex class.
|
You need to overload your constructors.
class CComplex {
double r, i;
public:
explicit CComplex(double real=0.0, double imag=0.0)
: r(real),i(imag) {}
explicit CComplex(const CComplex&t) : r(t.r) ,i(t.i) {}
explicit CComplex(int real, int imag=0) : r(real),i(imag) {}
explicit CComplex(float real, float imag=0.0f) : r(real),i(imag) {}
// more stuff...
};
The first constructor is the default constructor, because all arguments have
default values. It also accepts either one or two double values, so that
you're able to initialize TWO above.
The second constructor is a copy constructor, used whenever the compiler
needs to copy a CComplex object -- for instance when a function returns
CComplex by value.
Frankly, you don't need the other two constructors because ints and
floats can already be converted to double. I included them because it
demonstrates the technique you're looking for. Note that you can only
have one default constructor -- don't make the first argument optional
in any of the other constructors, or you'll get ambiguity errors.
You would probably want to do something similar with operator= as well.
See any book on C++ for examples on how to do this.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Fri Oct 29, 2004 2:40 pm Post subject: Re: How Do I overload (TYPE) operator? |
|
|
sean worlock wrote:
| Quote: | 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;
|
First of all, using ALL CAPS for variable is not good programming style.
Common practice is to use all caps identifiers for macros only.
| Quote: | I wish to perform the following...
ONE=(CComplex) value1;
TWO=(CComplex) value2;
THREE=(CComplex) value3;
|
C-style casts are also bad programming style, you should avoid them at
all costs. Use C++ casts, like static_cast<> for example. In this
particular case you don't even need casts as:
one = value1;
two = value2;
three = value3;
are perfectly valid, once you solve the conversion issue. If you really
want to stress the conversion you can use the "functional" style:
one = CComplex(value1);
two = CComplex(value2);
three = CComplex(value3);
| Quote: | 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?
|
The term "type operator" is not C++ terminology. I assume you meant
"conversion operator". What you need is not conversion operators, but
constructors. Just write:
class CComplex
{
public:
CComplex(int x) { /*...*/ }
CComplex(float x) { /*...*/ }
CComplex(double x) { /*...*/ }
/*...*/
};
HTH,
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|