Topic: Conversion operator question
Author: ball@cygany.Eng.Sun.COM (Mike Ball)
Date: 22 Feb 1994 22:13:14 GMT Raw View
In article 210294112034@bill-gibbons1.taligent.com, bgibbons@taligent.com (Bill Gibbons) writes:
>
> is not. This came up in committee discussions and it was decided that it
> should remain valid for a class to have a conversion operator which
> returns the class type itself.
There were multiple stages to this decision. In the not too distant past, conversions
to the type itself were disallowed by the DWP (Draft Working Paper). In the
last but one, they were allowed, but would never be used to convert to
a class of the same type or a reference of the same type. In the current draft,
the section has disappeared entirely. which I assume is an error.
Unfortunately, compilers are frozen at some point, and changes from the DWP
don't get propogated automatically. That's good, since it would have eliminated
such conversions entirely when the current draft came out. :-)
It's bad because questions like this come up.
I'm curious, though, about how the original poster intended to use the conversion
operator.
-Mike Ball-
SunPro
Author: clerc@gla.ecoledoc.ibp.fr (Fabrice Clerc)
Date: 26 Feb 1994 14:18:09 GMT Raw View
In article <2ke01q$fs5@engnews1.Eng.Sun.COM>, ball@cygany.Eng.Sun.COM (Mike Ball) writes:
|> I'm curious, though, about how the original poster intended to use the conversion
|> operator.
|>
|> -Mike Ball-
|> SunPro
|>
|>
The idea was to write something like:
#include <iostream.h>
class A;
class B;
class C;
class A {
public:
virtual operator B()=0;
};
class B: public A {
public:
B(int i=0) { i_=i; }
int i() { return i_; }
operator B() { return *this; }
private:
int i_;
};
class C: public A {
public:
operator B() { return B(1); }
};
int main() {
B b1;
C c1;
A& a1=b1;
A& a2=c1;
B b2=(B)a1;
B b3=(B)a2;
cout<<b2.i()<<endl<<b3.i()<<endl;
return 1;
}
some kind of generic cast system.
there are ways around the problem...
------ btw, how can I suppress the warning in the following code ? -----------
// "t2.cc", line 14: Warning (Anachronism): A::B is not accessible from file level.
// "t2.cc", line 14: Note: Type "CC -migration" for more on anachronisms.
class A {
private:
struct B {};
B* makeB(); // I don't want to define the method in the class
};
A::B* A::makeB() {
// lots of code
return new B;
}
(if you post an answer please email it to me also, thank you).
--
Fabrice Clerc
clerc@gla.ecoledoc.ibp.fr
Author: clerc@gla.ecoledoc.ibp.fr (Fabrice Clerc)
Date: 19 Feb 1994 16:17:26 GMT Raw View
class A {
operator A(); // (quite stupid anyway)
};
SunPro tells me it is not allowed (CC4.0 gives an error).
but IBM's xlC, Borland C++ 4.0, HP C++ 3.20 and gcc 2.5.8
accept it.
What does the ARM say about it ?
Thank you,
Fabrice
---
Fabrice Clerc
clerc@gla.ecoledoc.ibp.fr
--
Author: bgibbons@taligent.com (Bill Gibbons)
Date: Mon, 21 Feb 1994 19:24:37 GMT Raw View
In article <2k5e2m$hcv@vishnu.jussieu.fr>, clerc@gla.ecoledoc.ibp.fr
(Fabrice Clerc) wrote:
> class A {
>
> operator A(); // (quite stupid anyway)
> };
>
> What does the ARM say about it ?
The above case case may be stupid, but this one:
class B {
virtual operator D();
};
class D : B {
virtual operator D();
};
is not. This came up in committee discussions and it was decided that it
should remain valid for a class to have a conversion operator which
returns the class type itself.
Bill Gibbons
bgibbons@taligent.com