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

Discussion in 'C++' started by Alex Vinokur, Sep 24, 2003.

  1. Alex Vinokur

    Alex Vinokur Guest

    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:
    http://mathforum.org/library/view/10978.html
    =====================================
    Alex Vinokur, Sep 24, 2003
    #1
    1. Advertising

  2. Alex Vinokur

    Avinash Guest

    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" <> wrote in message news:<bkrehl$4htlf$-berlin.de>...
    > 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:
    > http://mathforum.org/library/view/10978.html
    > =====================================
    Avinash, Sep 24, 2003
    #2
    1. Advertising

  3. Avinash wrote:
    >
    > Alex,
    > Foo(i ) means 'i' is declared a variable of type Foo.



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

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 24, 2003
    #3
  4. Alex Vinokur

    jeffc Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    >
    >
    > Avinash wrote:
    > >
    > > Alex,
    > > Foo(i ) means 'i' is declared a variable of type Foo.

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


    That doesn't help the OP with his compiler error msg.
    jeffc, Sep 24, 2003
    #4
  5. Alex Vinokur

    Ron Natalie Guest

    "Karl Heinz Buchegger" <> wrote in message news:...
    >
    >
    > Avinash wrote:
    > >
    > > Alex,
    > > Foo(i ) means 'i' is declared a variable of type Foo.

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


    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.
    Ron Natalie, Sep 24, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    375
    John Roth
    Jul 29, 2005
  2. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    382
    JaredGrubb
    Sep 24, 2008
  3. Replies:
    1
    Views:
    65
    Benjamin
    Sep 9, 2006
  4. ceh

    OOP var foo vs this.foo

    ceh, Nov 23, 2007, in forum: Javascript
    Replies:
    6
    Views:
    121
    Richard Cornford
    Dec 12, 2007
  5. Replies:
    4
    Views:
    135
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page