This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Class casting
- From: LLeweLLyn Reese <llewelly at lifesupport dot shutdown dot com>
- To: Greg Davey <mailforroot at turbodiesel360 dot cjb dot net>
- Cc: gcc-help at gcc dot gnu dot org
- Date: 23 Jul 2003 06:54:46 -0700
- Subject: Re: Class casting
- References: <1057162170.13111.18.camel@davey-world.cjb.net>
Greg Davey <mailforroot@turbodiesel360.cjb.net> writes:
> Is there a way to force class casting in g++? Given the code below, some
> compilers will automatically cast class B to it's base-class using the
> function call in main(). Is there a way to make gcc recognize and/or
> force this cast? Any help would be appreciated. Than you in advance.
>
>
> class A{
> public:
> A(){}
> virtual void doStuff()=0;
> };
> class B : class A{
class B : public A {
You must use public inheritance to get B => A implicit conversions. If
other compilers do those implicit conversions with private
inheritance, they are wrong. Also, the second 'class' is uncessary
here.
> public:
> B():A(){}
> void doStuff(){
> std::cout << "Class B";
> }
> };
> class C{
> public:
> C(A& a) : initialize....{do Stuff;}
C(A const& a) {}
You cannot bind a temporary to a reference to non-const. 'B()' creates
a temporary of type B.
If you feel you need to include non-code comments like 'initialize
.....', please put them in comments so people trying to help you
can compile your code and get only the errors which are troubling
you, and not a dozen irrevelant errors.
> void doOtherStuff(){
> a.doStuff();
This class hasn't got a member named 'a'.
> }
> };
>
> int main(){
> C* c = new C(B());
> c->doOtherStuff();
> return 0;
> }