473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Issue in casting

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
Oct 10 '08 #1
3 1227
On Oct 10, 11:30*am, Alexander Adam <cont...@emiasys.comwrote:
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.
Oct 10 '08 #2
On 10 Okt., 16:30, Alexander Adam <cont...@emiasys.comwrote:
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
Oct 10 '08 #3
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.

Oct 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jakob Bieling | last post by:
Hi, Suppose I have a base class and a derived class as follows: class base { public: void somefunc (); };
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
12
by: curious1 | last post by:
Can someone tell me how the 'is' command works in relation to derived classes? For example - Class A {} Class B : A {} object o = new B(); if (o is A) {...}
17
by: sureshjayaram | last post by:
I have problem with the code only with 64 bit big endian machine. i have a member size_t val1 in my structure. The size of size_t is 32(unsigned int) in 32 bit machines and 64(unsigned long) in 64...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
2
by: | last post by:
Hi all, I have the following objects and namespaces namespace MyNameSpace.Borrowers { public class Borrower() { }
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
4
by: harsh.murari | last post by:
In the example below, a single object (Network) is implementing 2 interfaces (INetworkA and INetworkB). Both of these interfaces derive from the IBase interface (similar to the IUnknown interface...
4
by: AndreH | last post by:
Good day, I have the a bit of an issue with retrieving an object from php's session. I set a session variable "user" from the class User as follows: $user = new User(); // ... do some stuff...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.