473,405 Members | 2,373 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,405 software developers and data experts.

downcasting in c++ I couldnt success

class A {
..
};
class B : public A
{
..
};

int main( )
{
stack<Amystack;
A a = new B( );
mystack.push(a);

A newA = mystack.top( );
B b = (B) newA; //doesnt work here
return 0;
}

how could I do downcast???
Oct 4 '08 #1
19 2481
eMRe <em********@gmail.comwrites:
class A {
[snip]

You're looking for comp.lang.c++.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 4 '08 #2
On 4 Oct 2008 at 21:10, eMRe wrote:
class B : public A
[snip]
A newA = mystack.top( );
B b = (B) newA; //doesnt work here

how could I do downcast???
Since B is derived from A, in general it doesn't make sense to cast from
A to B.

If you happen to know that the object you've popped is actually a B,
then you can convert (a pointer to the object) with dynamic_cast. (If
the cast isn't valid, then dynamic_cast will return NULL.)

Oct 4 '08 #3
eMRe wrote:

Please don't multi-post on Usenet.
>
how could I do downcast???
What was wrong with the answer you got in the appropriate group?

--
Ian Collins.
Oct 4 '08 #4
Antoninus Twink wrote:
On 4 Oct 2008 at 21:10, eMRe wrote:
>class B : public A
[snip]
> A newA = mystack.top( );
B b = (B) newA; //doesnt work here

how could I do downcast???

Since B is derived from A, in general it doesn't make sense to cast from
A to B.

If you happen to know that the object you've popped is actually a B,
then you can convert (a pointer to the object) with dynamic_cast. (If
the cast isn't valid, then dynamic_cast will return NULL.)
Both off topic and plain wrong.

You know the post is OT here and you're being an even bigger wanker than
usual by posing a misleading response.

--
Ian Collins.
Oct 4 '08 #5
Antoninus Twink wrote, On 04/10/08 22:37:
On 4 Oct 2008 at 21:10, eMRe wrote:
>class B : public A
[snip]
> A newA = mystack.top( );
B b = (B) newA; //doesnt work here

how could I do downcast???

Since B is derived from A, in general it doesn't make sense to cast from
<snip>

The OP needs to be aware that whilst it is possible that what Antoninus
Twink says is correct it is also entirely possible that it is compete
rubbish but that the people here (who are C experts and not necessarily
C++ experts) won't be able to correct it. However, if the question is
asked in the correct group, i.e. comp.lang.c++ there will definitely be
people who can correct any miss-information unlike here.

Antoninus Twink is doing you (the OP) a dis-service by failing to tell
you where the experts in C++ are and doing this group a dis-service by
trying to encourage you and others to ask about C++ in this group which
is about C.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 4 '08 #6
On 4 Oct 2008 at 22:11, Ian Collins wrote:
Antoninus Twink wrote:
>On 4 Oct 2008 at 21:10, eMRe wrote:
>>class B : public A
[snip]
>> A newA = mystack.top( );
B b = (B) newA; //doesnt work here

how could I do downcast???

Since B is derived from A, in general it doesn't make sense to cast from
A to B.

If you happen to know that the object you've popped is actually a B,
then you can convert (a pointer to the object) with dynamic_cast. (If
the cast isn't valid, then dynamic_cast will return NULL.)
Both off topic and plain wrong.
Whether it's off topic is a matter of opinion.

If it's wrong, why not point out the error instead of hurling mud?

I freely admit that I'm not a C++ expert, but I know enough to get by,
and given that the people here who *are* C++ experts will refuse to help
because they'd rather play topicality games, I'll give it my best shot.

I think the following is true:
1) What I've said above is correct (assuming A is polymorphic)
2) Using a dynamic_cast instead of a static_cast is safer in the
situation above, where error-checking that what comes off mystack really
is of type B could be valuable.

Oct 5 '08 #7
Antoninus Twink wrote:
On 4 Oct 2008 at 22:11, Ian Collins wrote:
>Antoninus Twink wrote:
>>On 4 Oct 2008 at 21:10, eMRe wrote:
class B : public A
[snip]
A newA = mystack.top( );
B b = (B) newA; //doesnt work here

how could I do downcast???

Since B is derived from A, in general it doesn't make sense to cast
from A to B.

If you happen to know that the object you've popped is actually a B,
then you can convert (a pointer to the object) with dynamic_cast.
(If the cast isn't valid, then dynamic_cast will return NULL.)
Both off topic and plain wrong.

Whether it's off topic is a matter of opinion.
No, it is not. C is not C++ and C++ is not topical at all in comp.lang.c
This is not a matter of opinions, just a fact.

Bye, Jojo
Oct 5 '08 #8

"Antoninus Twink" <no****@nospam.invalidwrote in message
Ian Collins
>>
Both off topic and plain wrong.

Whether it's off topic is a matter of opinion.

If it's wrong, why not point out the error instead of hurling mud?
Some regulars have a rather narrow view of topicality.

That doesn't mean that there's no such thing. Libertarians have a very
narrow view of what governemnts should do. However unless you are some sort
of fanatical Marxist, you will agree that there are some things that
governments shouldn't do.

The C++ casting rules are off-topic here and belong in comp.lang.c++. Claims
that replies are wrong are also off-topic, though by replying with off-topic
information you've created an impossible situation, because no-one wants to
see incorrect information go unchallenged.

In my understanding of C++ you are in fact right, but it's a long time since
I used C++. Intricate casting rules were one of the things that put me off.

Incidentally certain words are also inappropriate for the newsgroup. Some
hobby programmers are children, and we should be careful what we expose
young people to.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 5 '08 #9
Joachim Schmitz wrote:
Antoninus Twink wrote:
Whether it's off topic is a matter of opinion.

No, it is not. C is not C++ and C++ is not topical at all in
comp.lang.c This is not a matter of opinions, just a fact.
Fortunately, the OP proved to be smarter than Twink (admittedly not
much of a benchmark) and reposted the question in proper newsgroup.

Brian
Oct 5 '08 #10
Antoninus Twink wrote:
On 4 Oct 2008 at 22:11, Ian Collins wrote:
>Antoninus Twink wrote:
[wrong C++ advice]
>Both off topic and plain wrong.

Whether it's off topic is a matter of opinion.
Can you read? Where's the "C++" in this groups name?
If it's wrong, why not point out the error instead of hurling mud?
If you were to follow the example of the OP an ask in an appropriate
place, I might.

--
Ian Collins.
Oct 5 '08 #11
On 5 Oct 2008 at 17:09, Malcolm McLean wrote:
Some regulars have a rather narrow view of topicality.
Is that the famous British understatement?
Incidentally certain words are also inappropriate for the newsgroup.
Some hobby programmers are children, and we should be careful what we
expose young people to.
I'm not sure exactly what I said that was offensive... I rarely use
strong language, though it's possible that CBF's bloody-minded ignorance
has pushed me to it once or twice.

Although it's hard to believe they'll hear anything in clc remotely
comparable to the unmitigated filth that flies around most
schoolyards...

Oct 5 '08 #12
Ian Collins wrote:
Antoninus Twink wrote:
>>
If it's wrong, why not point out the error instead of hurling mud?

If you were to follow the example of the OP an ask in an appropriate
place, I might.
"Never mud-wrestle with a pig. You both get dirty,
and the pig enjoys it."

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 5 '08 #13
On 5 Oct 2008 at 21:07, Eric Sosman wrote:
Ian Collins wrote:
>Antoninus Twink wrote:
>>If it's wrong, why not point out the error instead of hurling mud?

If you were to follow the example of the OP an ask in an appropriate
place, I might.

"Never mud-wrestle with a pig. You both get dirty,
and the pig enjoys it."
Don't worry, I have no intention of playing Collins' games.

Oct 5 '08 #14

"Antoninus Twink" <no****@nospam.invalidwrote in message
On 5 Oct 2008 at 17:09, Malcolm McLean wrote:
>Some regulars have a rather narrow view of topicality.

Is that the famous British understatement?
>Incidentally certain words are also inappropriate for the newsgroup.
Some hobby programmers are children, and we should be careful what we
expose young people to.

I'm not sure exactly what I said that was offensive... I rarely use
strong language, though it's possible that CBF's bloody-minded ignorance
has pushed me to it once or twice.

Although it's hard to believe they'll hear anything in clc remotely
comparable to the unmitigated filth that flies around most
schoolyards...
You quoted a rude word instead of asterisking it out.

Whilst children certainly know these words, sometimes know what they mean,
and use them amongst themselves, they shouldn't be exposed to adults using
them in their presence, nor should adults accept swear words from children.
It's a sort of inter-generational compact. On clc, we should remember that
everyone can access the ng.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 5 '08 #15

"Antoninus Twink" <no****@nospam.invalidwrote in message
On 5 Oct 2008 at 17:09, Malcolm McLean wrote:
I'm not sure exactly what I said that was offensive...
Actually I checked back in the thread and you snipped. I got mixed up as to
who quoted whom.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 5 '08 #16
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>On 5 Oct 2008 at 17:09, Malcolm McLean wrote:
>Some regulars have a rather narrow view of topicality.

Is that the famous British understatement?
>Incidentally certain words are also inappropriate for the newsgroup.
Some hobby programmers are children, and we should be careful what we
expose young people to.

I'm not sure exactly what I said that was offensive... I rarely use
strong language, though it's possible that CBF's bloody-minded ignorance
has pushed me to it once or twice.

Although it's hard to believe they'll hear anything in clc remotely
comparable to the unmitigated filth that flies around most
schoolyards...
Well, I know that we are not supposed to use "the S word" in this
newsgroup. I assume that we all know what the S word is; I don't think
I have to explain further.

I assume that's what Malcolm was referring to.

Oct 6 '08 #17
Malcolm McLean wrote:
>
.... snip ...
>
Incidentally certain words are also inappropriate for the
newsgroup. Some hobby programmers are children, and we should
be careful what we expose young people to.
Examples include "Antoninus", "Twink", "Kenny McCormack".

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 6 '08 #18
Kenny McCormack <ga*****@shell.xmission.comwrote:
>
Well, I know that we are not supposed to use "the S word" in this
newsgroup. I assume that we all know what the S word is; I don't think
I have to explain further.
SN*BOL? S*mula? Sm*lltalk? Sch*me? ;-)
--
Larry Jones

My dreams are getting way too literal. -- Calvin
Oct 6 '08 #19
la************@siemens.com wrote:
Kenny McCormack <ga*****@shell.xmission.comwrote:

Well, I know that we are not supposed to use "the S word" in this
newsgroup. I assume that we all know what the S word is; I don't think
I have to explain further.

SN*BOL? S*mula? Sm*lltalk? Sch*me? ;-)
St*nd*rd :-(

Richard
Oct 7 '08 #20

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

Similar topics

0
by: Stephan Deibel | last post by:
Hi, O'Reilly Associates has agreed to print a second volume of Python Success Stories and I am looking for contributors of new stories. This booklet will showcase Python in the context of a...
6
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I...
4
by: Bilgehan.Balban | last post by:
Hi, Generally in my C code, I use the following convention to return success or failure: a) For functions that return a pointer, (allocation, filling-in a structure etc.) return Null for all...
1
by: audipen | last post by:
Hi, I trap the 'OnBuildProjConfigDone' and perform some custom enhancement to the assembly. I check the 'bool Success' parameter before I perform my step. I go ahead only if 'Success == true" ...
6
by: A.M-SG | last post by:
Hi, We are developing a SmartClient application and we are planning to expose business objects layer to SmartClient application by using ASP.NET SOAP web services.
1
by: nmn | last post by:
thank you in advance.. i am trying to retrive data to my txt fields from access tables by using dbcombo i am using DATA1 for the datasource and "car" for the table. i have no problem in listing...
5
by: Ryan van Roode | last post by:
Hello, An API function gives me an object of class 'Blah'. I have subclassed 'Blah' and added a few methods: class EnhancedBlah extends Blah { function enhancement1() {} function...
5
by: emre esirik(hacettepe computer science and enginee | last post by:
/////////////////////// DIVISION FUNCTION //////////////////// void div ( int pol1 , int pol1p , int pol2 , int pol2p) { int r=5,z,i,g,j,k,l,dividend,rising={0}; for (i=0 ; i<6 ; i++)...
2
by: emre esirik(hacettepe computer science and enginee | last post by:
struct guitar active; char *buffer_char3; while(!feof(ptdata)) { fgets(buffer,26, ptdata); fgets(buffer,26, ptdata); fgets(buffer,26, ptdata); fgets(buffer,26, ptdata); fgets(buffer,26,...
9
snowfall
by: snowfall | last post by:
What is downcasting and how to implement it?? Am not getting any tutorial on this... pls help...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.