downcasting in c++ I couldnt success

E

eMRe

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

int main( )
{
stack<A> mystack;
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???
 
A

Antoninus Twink

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

Ian Collins

Antoninus said:
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.
 
F

Flash Gordon

Antoninus Twink wrote, On 04/10/08 22:37:
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.
 
A

Antoninus Twink

Antoninus said:
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.
 
J

Joachim Schmitz

Antoninus said:
Antoninus said:
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
 
D

Default User

Joachim said:
Antoninus Twink wrote:

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
 
I

Ian Collins

[wrong C++ advice]
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.
 
A

Antoninus Twink

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

Kenny McCormack

Is that the famous British understatement?


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

CBFalconer

Malcolm said:
.... 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".
 
L

lawrence.jones

Kenny McCormack said:
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? ;-)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top