[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Issue in casting

Alexander Adam

10/10/2008 2:31:00 PM

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
3 Answers

Darío

10/10/2008 2:47:00 PM

0

On Oct 10, 11:30 am, 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).

Why do you want to down cast?
That's a good question.

peter koch

10/10/2008 5:21:00 PM

0

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

Rolf Magnus

10/10/2008 8:52:00 PM

0

Alexander Adam 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:

You can't. You only returned a copy of the A part of your object from the
function, so an A is all that's left of it.

> ...
> 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).

Why? Returning a pointer to a dynamically allocated B would be a solution.
As long as you return by value, there is nothing you can do to get what you
want.