How to scare off a Java developer without a pointer ?

Ask him this. 

What will the following program print ? 

struct counter
{
    int val;
    counter():val(0){}
};
int main()
{
    std::vector<counter> counters;
    counters.push_back(counter());
    counters.push_back(counter());
    for(int i = 0; i < counters.size(); i++){
        counter t = counters[i];
        t.val++;
    }
    std::cout << counters[0].val << std::endl;
    return 0;
}

If he says zero ask him 🙂 how you will make it to print 1 with a single line change ?Don’t  allow him to 1) change the std::cout line 2) change the initial value of counter.val. Watch him curse C++ 🙂

 

Advertisements

About Amar Deka

Software Engineer
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to How to scare off a Java developer without a pointer ?

  1. Eli Tilevich says:

    Insert an & between counter and t on line 12: counter & t = counters[i];
    But I used to be a commercial C++ programmer in the 90’s.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s