Jump to content
  • Advertisement
  • entries
    146
  • comments
    435
  • views
    198844

The second quiz!

Sign in to follow this  
Washu

177 views

Well, ok, I lied. This quiz was a lot harder than the previous one. Obviously the first one required you to know the insides of multiple inheritance, while the second one was all about templates and the difference between complete and incomplete types.

Ok, so first up is question 1.
1.1) typeid(*a); is well formed. It will return the typeid of SubBase2.
1.2) dynamic_cast(a); is well formed as well, as a is of type Base* and SubBase2 is derived from Base. Results in a SubBase2*.
1.3) typeid(*b); is malformed and results in undefined behavior. Since we are in the constructor of SubBase2, type SubBase1 is not a base of SubType2.
1.4) dynamic_cast(b); is malformed and results in undefined behavior. Since we are in the constructor of SubBase2, type SubBase1 is not a base of SubType2
1.5) a->Arr(); Well formed, calls SubBase2::Arr().
1.6) b->Arr(); Malformed, undefined behavior. SubBase1 is not a base of SubBase2.

This example was almost an exact copy of two similar examples from the standard, in section 12.7. When typeid or dynamic_cast is used in a constructor or in a destructor, or in a function called from either, and if the operator of typeid/dynamic_cast refers to the object under construction or destruction, and the static type of the operand is neither the constructor nor the destructors class, nor one of its bases, then the behavior is undefined.

Question 2:
2.1) X* p; Is well formed, pointers to incomplete types are allowed.
2.2) X a; Is malformed, this results in infinite recursion and hence the result is undefined behavior.

This one was an exact copy of the standard, from section: 14.7.1 -14.

It's not really all that surprising that such detail isn't typically known to people. However, it is these details that tend to nail people in the end. Especially of concern are constructors and destructors. These member functions of classes behave differently than all other member functions, and as such the rules of their behavior should be well understood before hand.
Sign in to follow this  


4 Comments


Recommended Comments

Interesting.

I've got a question though... You (as well as others) refer to the C++ standard (presumably the latest ISO one?). Have you got this in print form, or are you looking it up online?

Just wondering if it's a reference I should add to my list of things-to-buy [smile]

cheers,
Jack

Share this comment


Link to comment
I bought the PDF version. You can get it from the ANSI site and the ISO site.

Share this comment


Link to comment
In the UK you can buy a hard bound volume of it (check Amazon), handy for looking things up and beating away rabid Pascal programmers [grin]

Share this comment


Link to comment
Well I'll add it to my list of things to check out then [smile]

There's a whole bunch of books I want to pick up and read after I'm done with Code Complete. Meyer's and Sutter's books for a start - I've liked the ones I've already read.

Cheers
Jack

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!