Topic: Overloading assign?
Author: nevries@cs.ruu.nl (Nico de Vries)
Date: 22 Oct 91 15:43:08 GMT Raw View
I want to overload assign. Not the ordinary way,
<new type> = <existing type>. But <existing type> = <new type>.
Actually both at once. Stroustrups C++ book says (as I think
he means) this is not possible. Assignment can only
be overloaded with a member of a class. Unfortunately my C++
compiler ensures my assumption.
Is there a trick around this. (to an expert this might be
a dumb question, please make me smarter)
Nico de Vries MAIL: nevries@praxis.cs.ruu.nl
---
#include <dislaimer&MY opinion.h++>
"I know my English spelling is bad, but if I used my " O O
"own language you probably couldn't read it at all. " |
(BTW I prefer email replys) \_/
Author: hf@informatik.uni-kl.de (Harald Fuchs)
Date: 23 Oct 91 12:51:49 GMT Raw View
In article <1991Oct22.154308.6660@cs.ruu.nl> nevries@cs.ruu.nl (Nico de Vries) writes:
I want to overload assign. Not the ordinary way,
<new type> = <existing type>. But <existing type> = <new type>.
Actually both at once. Stroustrups C++ book says (as I think
he means) this is not possible. Assignment can only
be overloaded with a member of a class. Unfortunately my C++
compiler ensures my assumption.
If I got that right, you don't really want to overload the assignment
operator; you just want some automagical type conversion from one of
your types to an existing type you won't touch. This can be
accomplished by a user-defined type conversion like that:
NewType::operator ExistingType () const
Is there a trick around this. (to an expert this might be
a dumb question, please make me smarter)
There are no dumb questions, there are only dumb answers. But I'm not
an expert...
--
Harald Fuchs <hf@informatik.uni-kl.de>
Author: markr@cs.liv.ac.uk (Mark Rivers)
Date: 29 Oct 91 14:40:36 GMT Raw View
nevries@cs.ruu.nl (Nico de Vries) writes:
> I want to overload assign. Not the ordinary way,
> <new type> = <existing type>. But <existing type> = <new type>.
> Actually both at once. Stroustrups C++ book says (as I think
> he means) this is not possible. Assignment can only
> be overloaded with a member of a class. Unfortunately my C++
> compiler ensures my assumption.
>
> Is there a trick around this. (to an expert this might be
> a dumb question, please make me smarter)
Maybe you could use an overloaded = for <newtype> = <existingtype>,
and an overloaded cast operator for <existingtype> = <newtype>.
Eg.
class NewType {
// whatever
public:
NewType& operator =(int); // assign newtype = int
operator int(); // turn into an int
};
main() {
int x;
NewType n;
n = x; // same as n.operator=(x);
x = n; // same as x = n.operator int();
}
See ARM section 12.3.2, page 272.
Disclaimer - better experts than me may stomp all over this idea... :-)
Mark Rivers
Liverpool Uni Comp Sci Dept.
Author: drm@cats.com (Derek R. Marsano)
Date: 31 Oct 91 22:49:52 GMT Raw View
#if 0
In article <1991Oct29.144036.775@and.cs.liv.ac.uk> markr@cs.liv.ac.uk (Mark Rivers) writes:
>nevries@cs.ruu.nl (Nico de Vries) writes:
>
>> I want to overload assign. Not the ordinary way,
>> <new type> = <existing type>. But <existing type> = <new type>.
>> Actually both at once.
>> [text deleted]
>
>Maybe you could use an overloaded = for <newtype> = <existingtype>,
>and an overloaded cast operator for <existingtype> = <newtype>.
>
> [sample code deleted]
>
>See ARM section 12.3.2, page 272.
>
>Disclaimer - better experts than me may stomp all over this idea... :-)
>
>Mark Rivers
>Liverpool Uni Comp Sci Dept.
At the risk of joining you under the elephant's hoof, Mark, I present
the results I obtained from applying your suggestion. Basically, it
works!
However, I get a surprising warning when compiling with Sun CC 2.1. I
get no warning when compiling with g++ 1.37. Here it is..
#endif
#include <stdio.h>
class NewType
{
int v;
public:
void operator =(int); // assign newtype = int
operator int(); // turn into an int
void print(void);
};
void NewType::operator =(int i) { v = i + 1; }
NewType::operator int() { return(v); }
void NewType::print(void) { printf("NewType::v = %d\n", v); }
main()
{
int x = 100;
NewType n;
printf("x = %d\n", x);
n = x; // same as n.operator=(x);
x = n; // same as x = n.operator int();
n.print();
printf("x = %d\n", x);
}
#if 0
Here is the Sun CC compiler diagnostic and program output:
% CC 3982.cc
"3982.cc", line 21: warning: n used but not set
% a.out
x = 100
NewType::v = 101
x = 101
%
Can anybody decode the warning message?
#endif
Author: schierz@iobj.UUCP (Ruediger Schierz)
Date: 3 Nov 91 10:27:56 GMT Raw View
In article <1991Oct31.224952.26115@cats.com>, drm@cats.com (Derek R. Marsano)
writes:
>>nevries@cs.ruu.nl (Nico de Vries) writes:
>>
>>> I want to overload assign. Not the ordinary way,
>>> <new type> = <existing type>. But <existing type> = <new type>.
>>> Actually both at once.
>>> [text deleted]
>>
>>markr@cs.liv.ac.uk (Mark Rivers) writes:
>>Maybe you could use an overloaded = for <newtype> = <existingtype>,
>>and an overloaded cast operator for <existingtype> = <newtype>.
>>
>
>However, I get a surprising warning when compiling with Sun CC 2.1. I
>get no warning when compiling with g++ 1.37. Here it is..
>
>#include <stdio.h>
>
>class NewType
>{
> int v;
>public:
> void operator =(int); // assign newtype = int
> operator int(); // turn into an int
> void print(void);
>};
>
>void NewType::operator =(int i) { v = i + 1; }
>
>NewType::operator int() { return(v); }
>
>void NewType::print(void) { printf("NewType::v = %d\n", v); }
>
>main()
>{
> int x = 100;
> NewType n;
>
> printf("x = %d\n", x);
> n = x; // same as n.operator=(x);
> x = n; // same as x = n.operator int();
> n.print();
> printf("x = %d\n", x);
>}
>
>#if 0
>Here is the Sun CC compiler diagnostic and program output:
>% CC 3982.cc
>"3982.cc", line 21: warning: n used but not set
>% a.out
>x = 100
>NewType::v = 101
>x = 101
>%
>Can anybody decode the warning message?
>#endif
>
Derek, you get the warning because there is no constructor for NewType.
The assign-operator of class NewType should return something to allow
concatination:
class NewType {
int v;
public:
...
const NewType& operator=(int n) { v = n; return *this; }
...
};
This allows:
NewType x, y;
int n;
x = y = n;
To assign an object of a user-defined type to a built-in type you can also
overload the assign operator-function as a friend of class NewType
(it's not a member-operator but it behaves like a member-operator of int):
class NewType {
friend int operator=(int&, const NewType&);
...
};
int operator=(int& left, const NewType& right) // not a member-operator
{ left = right.v; // could be more complicated
return left;
}
main()
{
int a, b;
NewType n;
a = b = n;
}
But for this simple example it's the better idea to use the implicit
type conversion (operator int()). To assign to an integer you
have to convert your type to an integer anyway.
Using the implicit conversion you can use your type at every place where
you can use integers:
NewType n;
cout << n; // print like an int
--
mfG,
Rudiger Schierz
____
__/ iO \_________________________________Interactive Objects
\____/ Germany
Email: schierz@iobj.uucp
Phone: (+49)-7682-6374
Fax: (+49)-7682-6375