Thread View: comp.std.c++
1 messages
1 total messages
Started by Angelo Salvade
Fri, 19 Apr 1996 14:43
Are these "implicit" casts legal?
Author: Angelo Salvade
Date: Fri, 19 Apr 1996 14:43
Date: Fri, 19 Apr 1996 14:43
80 lines
2327 bytes
2327 bytes
Can anybody tell me if the following code is legal:
//----------------------------------------------------------------
class A {};
class B: public A {};
//----------------------------------------------------------------
// with templates
template <class O>
class Ref {
public:
Ref();
Ref(O* value);
operator O*();
};
extern void f1(Ref<A> a);
static void t1()
{
Ref<B> b;
Ref<A> a(b); // #1
f1(b); // #2
}
//----------------------------------------------------------------
// without templates
class RefA {
public:
RefA(A* value);
};
class RefB {
public:
RefB();
operator B*();
};
extern void f2(RefA a);
static void t2()
{
RefB b;
RefA a(b); // #3
f2(b); // #4
}
//----------------------------------------------------------------
I believe that #1-4 should work identically and are correct C++:
A Ref<A> should be initialised with a Ref<B>.
The following steps should happen:
- call of Ref<B>.operator B*()
- C++ built in conversion of B* to A*
- call of constructor Ref<A>(A*)
I checked this code with the Metrowerks CW8,
GNU 2.7.2 and SunPro C++ 4.0.1 & C++ 4.1 compilers.
The SunPro C++ 4.1 is the only compiler of those
that didn't compile #2 and #4. It gave the following message:
"Error: Cannot use Ref<B> to initialize Ref<A>".
Is my code illegal or is this a bug in the compiler?
Angelo Salvade
salvade@ecofin.ch
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads