Jump to content
  • entries
    146
  • comments
    436
  • views
    198709

A second quiz

Sign in to follow this  
Washu

576 views

The instructions for this quiz are quit simple: Unless otherwise specified assume that nothing is overloaded. Any missing components of a piece of code should be assumed to be there and in good working order. The questions in this one are a bit longer, and you should think carefully before answering. Use any reference materials you have, and please mention your sources if you do decide to use any, please don't read the answers others gave, as the last exam showed, many people who did so ended up with the wrong answers. Just because everyone believes something is true, doesn't make it true. There are no trick questions in this quiz, and any answers you make will most likely never be used against you in a court of law.

Question 1:
Using the code below as a reference, explain what behavior should be expected of each of the commented lines, please keep your answer very short.

struct Base {
virtual void Arr();
};

struct SubBase1 : virtual Base { };
struct SubBase2 : virtual Base {
SubBase2(Base*, SubBase1*);
virtual void Arr();
};

struct Derived : SubBase1, SubBase2 {
Derived() : SubBase2((SubBase1*)this, this) { }
};

SubBase2::SubBase2(Base* a, SubBase1* b) {
typeid(*a); //1
dynamic_cast(a); //2

typeid(*b); //3
dynamic_cast(b); //4

a->Arr(); //5
b->Arr(); //6
}










Question 2:
Using the code below as a reference, explain what behavior should be expected of each of the commented lines, please keep your answer very short.

template class X {
X* p; //1
X a; //2
};
Sign in to follow this  


8 Comments


Recommended Comments

1)
- 1. a is a pointer to a Base object
- 2. a is now casted to a SubBase2 object pointer
- 3. b is a pointer to a SubBase1 object
- 4. b fails because SubBase2 isn't derived from SubBase1
- 5. calls SubBase2::Arr()
- 6. calls Base::Arr()


2)
- 1. the variable p would be a pointer to a type T
- 2. the variable a would be a generic pointer so that container can hold the T class and derived classes(?)

And I know i got none of them right :(

EDIT: I used MSDN (not that it helped much).

Share this comment


Link to comment
I'm not going to pretend I knew the answer to #1. I don't think a lot of people do know, but I guess that's the point. I'm ashamed to say I looked it up in the standard, and I am shocked at how little I knew. I was halfway there, but totally didn't think of that, it seems obvious now[bawling]

I think I know Q2 though:

  1. Declares a pointer to an object of its own type.
  2. zOMG Breaks everything. X<T> contains an X<T*>, which contains an X<T**>, which contains an X<T***>, ad infinitum boredom.

Share this comment


Link to comment
Question 1
(used MSDN )
1. struct Derived
2. returns the pointer to the SubBase2 part of the Derived struct

3. struct Derived (not sure though, since SubBase1 has no virtual members except inherited from Base)
4. returns the pointer to the SubBase2 part of the Derived struct (not sure)

5. calls SubBase2::Arr
6. calls Base::Arr


Question 2
1. X<T>* p; This is a pointer to an object of type X<T>
2. X<T*> a; This is an object of type X<T*>

Share this comment


Link to comment
1) Derived
2) SubBase2* (aka 'this')
3) Derived
4) SubBase2* (aka 'this')
5) SubBase2::Arr
6) SubBase2::Arr

1) This is fine and commonly used in linked lists
2) This is not fine and will cause the compiler to violently explode

Share this comment


Link to comment
Okay, here's my attempt. I've not looked at the 2 comments above this one. References were the July '05 MSDN library on my machine - the template keyword, dynamic_cast<>() and typeid() pages. I probably should've used my copy of 'The C++ Programming Language', but I didn't.

Oh, and can I apply for "extenuating circumstances" before we get the marks? I'm not really feeling too intelligent today courtesy of having a night off with a bottle of red last night [lol]

Question 1

the exact results of typeid() are implementation specific...

1) Class SubBase1
2) Fails, if 'a' is SubBase1 then SubBase2 is side-by-side. Dynamic casting goes up/down the inheritance tree, not across?
3) Class Derived
4) Succeeds, down-casting.
5) Base::arr() is called. Goes to SubBase1 via the pointer, but SubBase1 doesn't overload Base's implementation
6) Same as with (5)

Question 2
I honestly don't know without compiling an example [headshake]
I'm thinking that (1) will be okay as you're declaring a recursive pointer, but not (2) as it's going to create an instance of itself.

Share this comment


Link to comment
*sigh* May as well get this over with. I have not read the other replies, nor have I fired up a compiler.
    1. Gets the type_info for SubBase2.
    2. Gets a SubBase2* which points to the SubBase2 part of Derived.
    3. Gets the type_info for SubBase1.
    4. Gets a SubBase2* which points to the SubBase2 part of Derived.
    5. Calls Base::Arr().
    6. Calls Base::Arr().

    1. Declares a pointer to another X<T> instance.
    2. DESTROYS THE UNIVERSE. (Looks like infinite template recursion to me, adding another pointer level to T each time.)
I just realized that construction order is relevant. God help us all.

Share this comment


Link to comment
1.1 Derived
1.2 SubBase2*
1.3 Derived
1.4 SubBase2*
1.5 Error
1.6 Error

2.1 Infinite Recursion
2.2 Infinite Recursion (never reached)

Share this comment


Link to comment
Question 1:

1. same as typeid SubBase1
2. non-null
3. same as typeid SubBase1
4. non-null
5. Base::Arr
6. Base::Arr

Question 2:

1. Self refering pointer, to class X where T is some type.
2. An object of it's own class X, where T is a pointer to some other T.

And thank you for reminding me why I'm moving away from C++ and pointers...

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!