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

C++ Quiz #4

Sign in to follow this  
Washu

171 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.




  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
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.

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!