Connecting Tech Pros Worldwide Help | Site Map

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

Alex Vinokur
Guest
 
Posts: n/a
#1: Jul 19 '05
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@connect.to
http://mathforum.org/library/view/10978.html
=====================================



Avinash
Guest
 
Posts: n/a
#2: Jul 19 '05

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$4htlf$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@connect.to
> http://mathforum.org/library/view/10978.html
> =====================================[/color]
Karl Heinz Buchegger
Guest
 
Posts: n/a
#3: Jul 19 '05

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
jeffc
Guest
 
Posts: n/a
#4: Jul 19 '05

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



"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3F7182B8.79956DFD@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.


Ron Natalie
Guest
 
Posts: n/a
#5: Jul 19 '05

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



"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message news:3F7182B8.79956DFD@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.


Closed Thread