Re: child and parent class args

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 03/30/05


Date: Wed, 30 Mar 2005 12:14:47 +0200

David Sobey wrote:

> hi
>
> Here's some code:
>
> void SomeFunc(childClass arg);
> ...
>
> parentClass a;
> a=new childClass();

a is an object of type parentClass. You can't assign a pointer to it, which
is what new returns. Also, note that a is already a full-blown object. No
need to create another one with new.

> SomeFunc(a);

I assume by parentClass, you mean a base class, and childClass is a class
derived from that base class. Then this won't work.

> this spits out an error, it can't convert an object of type parentClass to
> childClass.

It should give you more errors than that.

> Doesn't work for pointers either. Can anyone shed some light
> on how to get this working without casting?

It won't work with casting either. Ayway, what are you actually trying to
do? The above code doesn't make much sense. You can't just pass an object
of a base class to a function that expects an object of a class derived
from it, neither by value, nor by pointer or by reference. And casting is
not the solution.



Relevant Pages

  • Re: new versus override
    ... " So, if you override something, it will be accessible if you down ... down casting to base class. ... thus not accessible from an A pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: new versus override
    ... a.Method2(); // here is call to A's version of Method2 ... > down casting to base class. ... > thus not accessible from an A pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: the virtual keyword
    ... > you could not declare a body for it in the class it was declared virtual, ... This class can only be used as a base class, ... would result in a compiler error. ... function pointer that is added to the actual class. ...
    (comp.lang.cpp)
  • Re: Design problem
    ... Inspect the pointer, if it points to a Frog (or something that implements ... Or to stay with the example: Introducing a spline item. ... >> nor to have a fat interface at the base class. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Virtual inheritance...
    ... >> slower access to base class members and slower casts (including ... > Just as how an object contains a hidden pointer to a virtual function... ... an object with virtual functions will store ...
    (comp.lang.cpp)