Variable declaration taken as a function pointer declaration

Discussion in 'C++' started by Bolin, Dec 2, 2005.

  1. Bolin

    Bolin Guest

    When compiling the following code:

    Code (Text):

    #include <iostream>

    struct B {};

    struct A
    {
      A(B b1, B b2) {};
      void foo() { std::cout << "foo called" << std::endl; }
    };

    int main(int argc, char * argv[])
    {
      A a(B(), B());
      a.foo();
      return 0;
    }
    [\code]

    the compiler will interprete the first line of the main function as a
    function pointer declaration, and thus will fail at the next line. Does
    somebody know why this is so, and if there is an elegant way to solve
    this problem that does not involve temporary variables?

    B.
     
    Bolin, Dec 2, 2005
    #1
    1. Advertisements

  2.  
    Victor Bazarov, Dec 2, 2005
    #2
    1. Advertisements

  3. Bolin

    deane_gavin Guest

     
    deane_gavin, Dec 2, 2005
    #3
  4. Bolin

    Bolin Guest

    Thanks for your answer and your solution -- the thing that surprised me
    is that a bare B() could be interpreted as a function pointer.

    B.
     
    Bolin, Dec 2, 2005
    #4
  5. Bolin

    deane_gavin Guest

    Take a look at

    http://www.gotw.ca/gotw/075.htm

    particularly the logic up to examples 2d and 2e.

    Gavin Deane
     
    deane_gavin, Dec 2, 2005
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.