Topic: Overloaded type casting
Author: halgrim@diku.dk (Maz H. Spork)
Date: 1995/06/26 Raw View
alik@gauss.Stanford.EDU (Alik) wants to write the following <stripped> code:
>class A {
>...
>};
>class B : public A {
>...
>};
>class Collection {
>...
> void insert(A*);
> A* retrieve(...);
>...
>}
>operator B*(A *src) { doSomeStuff(); return (B*)src; }
>main()
>{
> B *b = new B;
> Collection c;
> c.insert(b);
> b1 = (B*)c.retrieve(...);
>}
>this gives him the error:
> `operator B *(A *)' must be a nonstatic member function
which is true.
>I can't make it a member of B, since then I would need to call
> (B*)(*c.retrieve(...)), instead of (B*)c.retrieve(...)
Try looking at it this way. If you know, in A, how to convert to a B, then
A is the place to put the conversion function --- ie. A::operator B*. Then
you would call retrieve on your Collection, yielding an A* which would
convert to a B* if that's the context.
What seems wrong is that you normally don't know this when writing your
base class. Why is it so important for you to cast back to some concrete
type? If you maintain a collection of A's then it seems wrong to retrieve
them as B's or C's. In general, casting "downwards" in the hierarchy using
conversion functions is a bad (meaning not very object-oriented) idea,
because the code you write is not very generic and is hard to maintain.
Hope this helps.
Maz
Author: alik@gauss.Stanford.EDU (Alik)
Date: 1995/06/26 Raw View
Maz H. Spork (halgrim@diku.dk) wrote:
: Try looking at it this way. If you know, in A, how to convert to a B, then
: A is the place to put the conversion function --- ie. A::operator B*. Then
: you would call retrieve on your Collection, yielding an A* which would
: convert to a B* if that's the context.
:
: What seems wrong is that you normally don't know this when writing your
: base class. Why is it so important for you to cast back to some concrete
: type? If you maintain a collection of A's then it seems wrong to retrieve
: them as B's or C's. In general, casting "downwards" in the hierarchy using
: conversion functions is a bad (meaning not very object-oriented) idea,
: because the code you write is not very generic and is hard to maintain.
My problem is that I have a fairly generic collection - say a factory which
keeps track of its creations. Now if I want to request a particular instance
by name, I know what it is (for example I know that I am requesting a
error handling stream), so I need to cast it. But to ensure that names do not
get confused, and that the cast is indeed waranted, I want to verify it.
I can't add the cast to a parent class, since I do not want to be modifying
my base classes every time a new object is introduced (to add a cast to it)
------------------------------------------------------------------
_/_/_/_/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/
_/_/_/_/ _/ _/ _/_/
_/ _/ _/ _/ _/ _/
_/ _/ _/_/_/_/ _/ _/ _/
alik@gauss.stanford.edu
------------------------------------------------------------------
Author: alik@gauss.Stanford.EDU (Alik)
Date: 1995/06/24 Raw View
Helo everyone.
I have a question about overloaded type casting operators.
Basic statement of the problem s the following: I have a collection of
objects (heterogenious). All objects have common base type A. So when I
call my collection's retrieve function, it returns me A*, which I need to
cast to the type I am expecting (assume I know what this particular query
should return).
I want to overload this cast, so that it does some type verification
(basically all types have uniques masks which can be checked - I am writing
a library, and so I need to check for application code abuse)
I want to write the following <stripped> code:
class A {
...
};
class B : public A {
...
};
class Collection {
...
void insert(A*);
A* retrieve(...);
...
}
operator B*(A *src) { doSomeStuff(); return (B*)src; }
main()
{
B *b = new B;
Collection c;
c.insert(b);
b1 = (B*)c.retrieve(...);
}
this gives me the error:
`operator B *(A *)' must be a nonstatic member function
I can't make it a member of B, since then I would need to call
(B*)(*c.retrieve(...)), instead of (B*)c.retrieve(...)
Finally there is a potential problem with infinite recursion [return (B*)src]
I know that there are other approaches, but they do not work quite as nicely.
So my question is: is there a way to overload the type casting operator in
such a way? (Please bear in mind, that there can be lots of inherited classes,
and I do not want ot modify A every time I add a new class to its children)
BTW, please CC me the reply directly, since I find it very difficult to read
all the messages that pass through this mailing list - and I do not want to
miss it.
TIA!
------------------------------------------------------------------
_/_/_/_/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/
_/_/_/_/ _/ _/ _/_/
_/ _/ _/ _/ _/ _/
_/ _/ _/_/_/_/ _/ _/ _/
alik@gauss.stanford.edu
------------------------------------------------------------------