Issue in casting

A

Alexander Adam

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
 
D

Darío

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

peter koch

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
 
R

Rolf Magnus

Alexander said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top