C++ Quiz #4

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
1 likes 9 comments

Comments

Migi0027

TFwibTx.png

I realize that this was imported from your blog. smile.png

November 18, 2014 12:15 PM
jpetrie

This one is easy. None of it even compiles. Done.

Where's my prize?

November 18, 2014 05:19 PM
unbird

Yeah, the "new" editor is really great.

November 18, 2014 07:45 PM
Washu
*sigh* fucking ... i don't even.

I mean REALLY?
November 19, 2014 04:23 AM
unbird

Yup, really. No more HTML tags for, erm, three years now. Only BBCode.

November 19, 2014 06:58 PM
Washu

Yup, really. No more HTML tags for, erm, three years now. Only BBCode.

This entry was imported from my blog. Apparently my choices for RSS import are either "Import everything completely fucked up and wrong." or "Import everything completely fucked up and wrong."
November 20, 2014 05:11 AM
unbird

Reminds me of the South Park episode about a mascot election.

There are HTML to BBCode translators online - not very reliable, BBCode don't seem to be standardized, every page has different flavours. And I've yet to find the escape sequence working here :/

November 20, 2014 11:16 AM
Washu
I don't think you quite understand.

This post was automatically imported from my blog.

I did not create this post (on this site).

I'm not going to bother editing it to fix it every single time it imports a post from my site.

Furthermore, it used to import them "just fine", with the exception of not properly handling formatting.
November 21, 2014 02:49 AM
unbird
Hmmm, now I see it. That bug doesn't seem to be that old (your older blog entries from this year worked). wacko.png

Yeah, such functionality should just work (you're not the only one using it). And there are years old issues with the editor. It's really a pity for a tech site like gamedev. No idea, why they don't get fixed.

Edit: Sorry for being off topic, this supposed to be about C++.
November 21, 2014 07:49 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement