peter koch
10/10/2008 5:21:00 PM
On 10 Okt., 16:30, Alexander Adam <cont...@emiasys.com> wrote:
> Hi all!
>
> Not sure what I am doing wrong so someone might be able to help me out
> here. I have a class model which looks like this:
>
> struct A
> {
> ..
>
> };
>
> struct B : public A
> {
> ..
>
> }
>
> Now I have a simple function which looks like this:
>
> A DoSomething()
> {
> return B(....);
>
> }
>
> And I am trying to cast it back to B from the outside:
>
> ...
> A myClass = DoSomething();
> B myClass2 = .... ??????
>
> I've tried dynamic_cast etc. but it doesn't seem to work. What I am
> doing wrong here? I'd like to avoid using dynamic_cast as I would like
> to avoid having to use pointers (i.e. return A* instead of A).
> Any idea?
>
> thanks!
> Alex
The problem is rather obvious, namely that DoSomething returns an
object of type A, not of type B. If you want to be able to return a B
object, you must return a pointer or a reference to A - not a value.
/Peter