3

What does this particular piece of code do? To be more precise, what does test tob(); do?

class test {
 private:
  int a;
  int b;
 public:
  test (int);
  test();
};
test::test() {
 cout<<"default";
}
test::test (int x=0) {
 cout<<"default x=0";
}
int main() {
 test tob();
}

I dont know what does test tob(); do, but it isn't giving any compilation errors.

aod
  • 77
  • 1
  • 2
  • 7

1 Answers1

9
test tob();

This declares a function which return type is test. It does not create an object. It's also know as most vexing parse.

To create a test object:

test tob;

Also, the way you define a function(include constuctor) with default argument is incorrect.

test::test (int x=0) {  // incorrect. You should put it in function when it's first declared
 cout<<"default x=0";
}

Below code should work:

class test {
  int a;
  int b;

 public:
  explicit test (int = 0);    // default value goes here
};

test::test (int x) {          
 cout<<"default x=0";
}

int main() {
 test tob;    // define tob object
}
billz
  • 44,644
  • 9
  • 83
  • 100
  • 3
    This is not what's known as *most* vexing parse. This is just vexing. :) –  Oct 11 '13 at 10:57
  • @hvd Absolutely but this piece of misinformation seems very presistent. I suppose ultimately words mean what people use them for, so in time this will be the most vexing parse, despite it not being nearly as vexing as the original. – john Oct 11 '13 at 11:31
  • 1
    Here's the real most vexing parse, http://en.wikipedia.org/wiki/Most_vexing_parse – john Oct 11 '13 at 11:34