Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    11
  • comments
    0
  • views
    180

C++ Quiz #3

Sign in to follow this  
Washu

57 views

This is a test of your knowledge of C++, not of your compiler's knowledge of C++. Using a compiler during this test will likely give you the wrong answers, or at least incomplete ones.



Given the following code:



class Base {  
public:
virtual ~Base() {}
virtual void DoSomething() {}
void Mutate();
};

class Derived : public Base {
public:
virtual void DoSomething() {}
};

void Base::Mutate() {
new (this) Derived; // 1
}
void f() {
void* v = ::operator new(sizeof(Base) + sizeof(Derived));
Base* p = new (v) Base();
p->DoSomething(); // 2
p->Mutate(); // 3
void* vp = p; // 4
p->DoSomething(); // 5
}



  1. Does the first numbered line result in defined behavior? (Yes/No)

  2. What should the first numbered line do?

  3. Do the second and third numbered lines produce defined behavior? (Yes/No)

  4. Does the fourth numbered line produce defined behavior? If so, why? If not, why?

  5. Does the fifth numbered line produce defined behavior? If so, why? If not, why?


  6. What is the behavior of calling void exit(int);?




Given the following code:



struct T{};  
struct B {
~B();
};

void h() {
B b;
new (&b) T; // 1
return; // 2
}



  1. Does the first numbered line result in defined behavior?

  2. Is the behavior of the second line defined? If so, why? If not, why is the behavior not defined?


  3. What is the behavior of int& p = (int)0;? Why does it have that behavior? Is this a null reference?


  4. What is the behavior of p->I::~I(); if I is defined as typedef int I; and p is defined as I* p;?




Source
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!