C++ Quiz #4

posted in Washu's Journal
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.




  1. What is the value of i after the first numbered line is evaluated?

  2. What do you expect the second numbered line to print out?

  3. What is the value of p->c after the third numbered line is evaluated?

  4. What does the fourth numbered line print?



    struct C;
    void f(C* p);


    struct C {
    int c;
    C() : c(1) {
    f(this);
    }
    };


    const C obj;
    void f(C* p) {
    int i = obj.c << 2; //1
    std::cout<< p->c << std::endl; //2
    p->c = i; //3
    std::cout<< obj.c << std::endl; //4
    }

  5. What should you expect the compiler to do on the first numbered line? Why?


  6. What should you expect the value of j to be after the second numbered line is evaluated? Why?



    struct X {
    operator int() {
    return 314159;
    }
    };
    struct Y {
    operator X() {
    return X();
    }
    };
    Y y;
    int i = y; //1
    int j = X(y); //2

  7. What should you expect the compiler to do on the first and second numbered lines? Why?



    struct Z {
    Z() {}
    explicit Z(int) {}
    };
    Z z1 = 1; //1
    Z z2 = static_cast<Z>(1); //2

  8. What should you expect the behavior of each of the numbered lines, irrespective of the other lines, to be?



    struct Base {
    virtual ~Base() {}
    };
    struct Derived : Base {
    ~Derived() {}
    };
    typedef Base Base2;
    Derived d;
    Base* p = &d;
    void f() {
    d.Base::~Base(); //1
    p->~Base(); //2
    p->~Base2(); //3
    p->Base2::~Base(); //4
    p->Base2::~Base2(); //5
    }



Source
Previous Entry C++ Quiz #3
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