[prev in list] [next in list] [prev in thread] [next in thread]
List: koffice-devel
Subject: Re: C++ help, please...
From: "Tomas Mecir" <mecirt () gmail ! com>
Date: 2006-05-30 13:22:27
Message-ID: 492258b10605300622m4c5f0755rafeca3e04e2f105d () mail ! gmail ! com
[Download RAW message or body]
On 5/30/06, Thomas Zander <zander@kde.org> wrote:
> On Tuesday 30 May 2006 14:34, Tomas Mecir wrote:
> > int main() {
> > C *c = new C;
> > A *a = dynamic_cast<A*>(c);
> > B *b = dynamic_cast<B*>(a);
> > cout << ((b != 0) ? "b is not NULL" : "b is NULL") << endl;
> > return 0;
> > }
>
> This worked here as well; hence my confusion.
>
> If you, however; pass 'a' to another class (in another file) and _there_
> cast it to B it will fail. Apparently C++ does more compile time then it
> should.
Update:
No, it does not. Just tried it, and it still works correctly.
I did the following:
- Moved definitions of A,B and C to test.h
- Made test2.h:
class A;
void fn (A *a);
- main now looks like this:
int main() {
C *c = new C;
A *a = dynamic_cast<A*>(c);
B *b = dynamic_cast<B*>(a);
cout << ((b != 0) ? "b is not NULL" : "b is NULL") << endl;
cout << "Now trying to pass to another function" << endl;
fn(a);
return 0;
}
- then I have test2.cc, which looks like this:
#include "test.h"
#include "test2.h"
#include <iostream>
using namespace std;
void fn (A *a) {
B *b = dynamic_cast<B*>(a);
C *c = dynamic_cast<C*>(a);
B *b2 = dynamic_cast<B*>(c);
cout << ((b != 0) ? "b is not NULL" : "b is NULL") << endl;
cout << ((c != 0) ? "c is not NULL" : "c is NULL") << endl;
cout << ((b2 != 0) ? "b2 is not NULL" : "b2 is NULL") << endl;
}
Output of all this is:
b is not NULL
Now trying to pass to another function
b is not NULL
c is not NULL
b2 is not NULL
It was compiled using:
gcc -c test.cc -o test.o
gcc -c test2.cc -o test2.o
gcc -o test test.o test2.o -lstdc++
So I still couldn't find a situation where this would fail :(
/ Tomas
_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic