>> we now encountered a problem which unfortunateley only occurs at runtime
I'm not sure you states what your problem is did you?
Main Topics
Browse All TopicsHi everybody,
in my company we just finished to port a lot of code (about 3 million lines) from VC++ 6.0 to VS 2008.
It mostly works fine but we now encountered a problem which unfortunateley only occurs at runtime - here's an example:
We have two classes, let's say A and B, both derived from the same base class C. Now we have a function like this:
void foo ( A* pA )
{
B* pB = dynamic_cast< B* >( pA );
if ( NULL == pB )
{
pA->DoSomething();
return;
}
pB->DoSomething();
}
I know, this is a very,very bad coding, I promise I would never do it, and I'm quite sure this is not allowed in C++ - but, the problem is we already found similar functions in our code, so there may be more, and they worked in VC 6++.
My question now is if anyone has an idea how it could be possible to find such places at compile time.
Thanks in advance,
ZOPPO
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi evilrix,
first thanks for the comments.
Well, if B is not derived from A it should return NULL, but in VC++ 6 it didn't, that's the problem. Now it returns NULL and the function doesn't do what it did before. That's our problem ...
In our code we use dynamic_cast very often, so just inspecting the code is nearly not possible (around 3700 calls in our main application).
Our class hierarchy is quite huge, and so we years ago decided to use dynamic_cast where it seems sensful to avoid the base classes become very huge due to a large number of virtual functions which only are needed for some derived classes.
Regards,
ZOPPO
>> VC++ 6 it didn't, that's the problem. Now it returns NULL and the function doesn't do what it did before
It didn't? That's odd ... as that's the whole point of dynamic_cast :) So, really it was behaving as static_cast so why not just change it for that then?
>> In our code we use dynamic_cast very often, so just inspecting the code is nearly not possible (around 3700 calls in our main application).
Wow, that suggests a serious design problem if you need to use dynamic cast so often!
>> Owe years ago decided to use dynamic_cast where it seems sensful to avoid the base classes become very huge
It would have probably been better to look at breaking the big classes up into smaller components. Based on this premise though, you'd want dynamic_cast to fail wouldn't you? Otherwise you'll not get polymorphic behavior.
So what is it you want to happen then? If there is no polymorphic relationship between A and B you want the code to fail to build? I'm not sure I see how that could work because clearly it is dynamic binding that is at play here, not static binding, and this isn't known until runtime (hence the existence of dynamic_cast).
The only thing I can think of at this time is you might be able to use a combination of boost type traits (is_polymorphic and is_base_of) and boost static assert (generates a build time assert) to confirm that A is a based of B and that A is a polymorphic type. It would, however, require you to modify all places you currently implement dynamic_cast for a different cast type.
I'll have a little play and see if I can knock you up some meta-template code to do this.
BTW: If A isn't polymorphic you should get error C2683 anyway...
http://msdn.microsoft.com/
...so I guess the problem is that A is polymorphic but is not a super class of B, yes?
Thanks again ...
maybe it wasn't clear enough - here a more detailed sample:
class C
{
...
};
class A : public C
{
void DoSomthingSpecialForA();
};
class B : public C
{
void DoSomthingSpecialForA();
};
voif foo ( A* pA )
{
B* pB = dynamic_cast< B* >( pA );
if ( NULL != pB )
{
pB->DoSomethingSpecialForB
return;
}
pA->DoSomethingSpecialForA
}
void test()
{
A a;
B b;
C* pA = &a;
C* pB = &b;
foo( (A*)pA ); // works in both VC++ 6 and VS 2008 since pA reall points to an A
foo( (A*)pB ); // works in VC++ 6 but not in VS 2008 since pB doesn't point to an A
}
In that sample in VC++ 6 both function calls to 'DoSomethingSpecial...' are made, one for a, one for b. In VS 2008 this won't do the expected ot might crash in the second call coz 'dynamic_cast< B* >' fails for 'pB' allthough it points to an instance of B. Since the dynamic_cast returns NULL the function handles pA as if it points to an A.
Please let me confirm once more that IMO that is terrible code, but until now we found two cases where exactly such thing was done (by developers which aren't in our team any longer - if they were I would for sure fire them now).
If our class hierarchy is of a bad design is a question beside this - as we started with the project 11 years ago we designed a class hierarchy with about 80 classes plus maybe 50 MFC-derived classes for the GUI, all together in 8 modules (DLLs), derived all from one base class and a set of sub-hierarchies - now, 11 years later the project grew up to 80 modules, about 3800 classes, maybe 1000 of them MFC derived, maybe again 1200 classes derived from our base class, the others are used for several different purposes.
As told, it's a huge project ... and, BTW, I don't think that 1 dynamic_cast per class is a general sign for bad design ;o)
ok, I hope I was clear enough now - and the goal for me would be to avoid to manually check all of these somethousand dynamic_casts if possible.
Best regards,
ZOPPO
PS: I'll leave office now but will be back tomorrow morning ...
Maybe this is relevant to the discussion: http://msdn.microsoft.com/
No, I think it's more correct than in VC++ 6, because it doesn't cast a pointer to A to a pointer to A if it points to B.
The above 'foo' would work if I first cast to the base class, i.e.:
voif foo ( A* pA )
{
B* pB = dynamic_cast< B* >( (C*)pA ); // cast to base class first
if ( NULL != pB )
{
pB->DoSomethingSpecialForB
return;
}
pA->DoSomethingSpecialForA
}
This works perfectly fine in VS 2008!
Bye ...
Hi evilrix,
I'll test your suggestion as soon as possible, but it may take some days, since I first need to discuss with out QA about replacing our older version of boost libraries (which doesn't contain the 'is_base_of') with a newer one.
I'll tell you about the result as soon as I tested it ...
Thanks a lot,
regards,
ZOPPO
Hi evilrix,
we now did some testing and found some kind of solution:
1. Your suggestion helped us to find 6 bugs where the cast-type and the expression of the dynamic_cast were unrelated - that's great. But this wasn't really the actual problem (which I maybe did not really declared comletely).
2. Then we thought about that stuff a little bit and found that in the case we had we at least could give a warning at compile time with a similar method if the cast-type and the casted expression are of exactly the same type - with this we found about 20 of those, most of them were harmless (and even not needed), 3 of them were bugs.
Thanks a lot,
best regards,
ZOPPO
Business Accounts
Answer for Membership
by: evilrixPosted on 2008-05-15 at 07:53:55ID: 21574289
>> I'm quite sure this is not allowed in C++
It's perfectly valid C++ (although not good practice). The dynamic cast will return NULL if B does not polymorphically (ie. at least 1 virtual function) derive from A.
>> My question now is if anyone has an idea how it could be possible to find such places at compile time.
Wouldn't it be better to just search the code for dynamic_cast (say, using grep) and take whatever action your coding standards define?
NB. The need to use dynamic_cast usually implies the class hierarchy is wrong.