Re: child and parent class args
From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 03/30/05
- Next message: Rolf Magnus: "Re: Translate C++ to C"
- Previous message: Peter Koch Larsen: "Re: Class hierarchy of exceptions (Ada, C++)"
- In reply to: David Sobey: "child and parent class args"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Rolf Magnus: "Re: Translate C++ to C"
- Previous message: Peter Koch Larsen: "Re: Class hierarchy of exceptions (Ada, C++)"
- In reply to: David Sobey: "child and parent class args"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|