Invoking constructor : Foo(var) and Foo ins(var)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Vinokur

    Invoking constructor : Foo(var) and Foo ins(var)

    Hi,

    What is wrong with this code?

    ====== C++ code : File t.cpp : BEGIN ======
    #include <iostream>
    using namespace std;

    struct Foo
    {
    Foo (int n) { cout << n << endl; }
    };

    int main ()
    {
    int i = 100; // Line#11

    Foo (i); // Line#13 : Compilation error
    Foo f (i); // Compiled with no errors

    Foo (200); // Compiled with no errors
    Foo (int(300)); // Compiled with no errors

    return 0;
    }
    ====== C++ code : File t.cpp : END ========


    ====== Compilation : BEGIN ======

    $ g++ -v
    [--omitted--]
    gcc version 3.3.1 (cygming special)

    $ g++ t.cpp

    t.cpp: In function `int main()':
    t.cpp:13: error: conflicting types for `Foo i'
    t.cpp:11: error: previous declaration as `int i'

    ====== Compilation : END ========


    =============== =============== =======
    Alex Vinokur
    mailto:alexvn@c onnect.to

    =============== =============== =======



  • Avinash

    #2
    Re: Invoking constructor : Foo(var) and Foo ins(var)

    Alex,
    Foo(i ) means 'i' is declared a variable of type Foo.
    but 'i' is already defined and C++ do now allow redeclaration.

    Thanks
    Avinash
    "Alex Vinokur" <alexvn@bigfoot .com> wrote in message news:<bkrehl$4h tlf$1@ID-79865.news.uni-berlin.de>...[color=blue]
    > Hi,
    >
    > What is wrong with this code?
    >
    > ====== C++ code : File t.cpp : BEGIN ======
    > #include <iostream>
    > using namespace std;
    >
    > struct Foo
    > {
    > Foo (int n) { cout << n << endl; }
    > };
    >
    > int main ()
    > {
    > int i = 100; // Line#11
    >
    > Foo (i); // Line#13 : Compilation error
    > Foo f (i); // Compiled with no errors
    >
    > Foo (200); // Compiled with no errors
    > Foo (int(300)); // Compiled with no errors
    >
    > return 0;
    > }
    > ====== C++ code : File t.cpp : END ========
    >
    >
    > ====== Compilation : BEGIN ======
    >
    > $ g++ -v
    > [--omitted--]
    > gcc version 3.3.1 (cygming special)
    >
    > $ g++ t.cpp
    >
    > t.cpp: In function `int main()':
    > t.cpp:13: error: conflicting types for `Foo i'
    > t.cpp:11: error: previous declaration as `int i'
    >
    > ====== Compilation : END ========
    >
    >
    > =============== =============== =======
    > Alex Vinokur
    > mailto:alexvn@c onnect.to
    > http://mathforum.org/library/view/10978.html
    > =============== =============== =======[/color]

    Comment

    • Karl Heinz Buchegger

      #3
      Re: Invoking constructor : Foo(var) and Foo ins(var)



      Avinash wrote:[color=blue]
      >
      > Alex,
      > Foo(i ) means 'i' is declared a variable of type Foo.[/color]


      No it doe not.
      It means to create an unnamed temporary object, initialize it with i
      and destroy it afterwards.

      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      • jeffc

        #4
        Re: Invoking constructor : Foo(var) and Foo ins(var)


        "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
        news:3F7182B8.7 9956DFD@gascad. at...[color=blue]
        >
        >
        > Avinash wrote:[color=green]
        > >
        > > Alex,
        > > Foo(i ) means 'i' is declared a variable of type Foo.[/color]
        >
        > No it doe not.
        > It means to create an unnamed temporary object, initialize it with i
        > and destroy it afterwards.[/color]

        That doesn't help the OP with his compiler error msg.


        Comment

        • Ron Natalie

          #5
          Re: Invoking constructor : Foo(var) and Foo ins(var)


          "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message news:3F7182B8.7 9956DFD@gascad. at...[color=blue]
          >
          >
          > Avinash wrote:[color=green]
          > >
          > > Alex,
          > > Foo(i ) means 'i' is declared a variable of type Foo.[/color]
          >
          >
          > No it doe not.
          > It means to create an unnamed temporary object, initialize it with i
          > and destroy it afterwards.[/color]

          No actually, Avinash is right. Syntactically this is ambiguous between
          being a declaration (Avinash) and an expression (Karl). The semantic
          rules say the declaration wins out. Foo (i) is the same as Foo i in this
          case.


          Comment

          Working...