C++ Quiz #3

Published November 18, 2014
Advertisement

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
Previous Entry C++ Quiz #2
Next Entry C++ Quiz #4
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement