Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

semantics???

1 view
Skip to first unread message

maverick

unread,
May 27, 2006, 6:46:26 AM5/27/06
to
Hi,

Please consider the following snippet:
#include<cstdio>
using std::printf;
class X
{
public:
int i;
private: //<<<<<(1)

X():i(1) { printf ( "CTOR\n");}
X(const X & ref ) { printf ( "CopyCtor\n" ); i = ref.i; }
~X() { printf ( "~dtor\n" ); }
};


int main()
{
X c(X()); //<<<<(2)
//X& ref = c; <<<<(3)
}

How the compiler views the statement at (2). The code, however, gets
compiled.
If I change 'private' to 'public' at (1), the code still compiles and
on running the binary there is no output.
It means that no CTOR call is required at (2)

If (3) is uncommented, the code fails to compile and the error message
is that c is not an lvalue.
Can someone please analyse why this happens?

Thanks


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Michael Tiomkin

unread,
May 28, 2006, 7:15:07 AM5/28/06
to
maverick wrote:
> Hi,
>
> Please consider the following snippet:
> #include<cstdio>
> using std::printf;
> class X
> {
> public:
> int i;
> private: //<<<<<(1)
>
> X():i(1) { printf ( "CTOR\n");}
> X(const X & ref ) { printf ( "CopyCtor\n" ); i = ref.i; }
> ~X() { printf ( "~dtor\n" ); }
> };
>
>
> int main()
> {
> X c(X()); //<<<<(2)

You declare a function named 'c' which has one 'X' argument and an
'X' return value.

> //X& ref = c; <<<<(3)
> }
>
> How the compiler views the statement at (2). The code, however, gets
> compiled.
> If I change 'private' to 'public' at (1), the code still compiles and
> on running the binary there is no output.
> It means that no CTOR call is required at (2)
>
> If (3) is uncommented, the code fails to compile and the error message
> is that c is not an lvalue.

A function name cannot be assigned in C/C++.

> Can someone please analyse why this happens?

This is a frequent mistake in C++, you can see other threads in this
group discussing it. In my opinion, the most simple solution is to
avoid using constructors inside declarations, e.g. in your case

X temp;
X c(temp);

Michael

Bo Persson

unread,
May 28, 2006, 7:15:53 AM5/28/06
to

"maverick" <mod...@gmail.com> skrev i meddelandet
news:1148719082....@i39g2000cwa.googlegroups.com...

> Hi,
>
> Please consider the following snippet:
> #include<cstdio>
> using std::printf;
> class X
> {
> public:
> int i;
> private: //<<<<<(1)
>
> X():i(1) { printf ( "CTOR\n");}
> X(const X & ref ) { printf ( "CopyCtor\n" ); i = ref.i; }
> ~X() { printf ( "~dtor\n" ); }
> };
>
>
> int main()
> {
> X c(X()); //<<<<(2)

This could be interpreted either as a variable c of type X,
initialized by an (unnecessary) default constructed X, *or* it could
be a declaration of a functions c, taking an unnamed parameter of
type X, and returning an X.

When in doubt, the compiler must treat it as a declaration. Because
the standard says so!


> //X& ref = c; <<<<(3)
> }
>
> How the compiler views the statement at (2). The code, however, gets
> compiled.
> If I change 'private' to 'public' at (1), the code still compiles
> and
> on running the binary there is no output.
> It means that no CTOR call is required at (2)

This is a hint that there is no variable at (2).

>
> If (3) is uncommented, the code fails to compile and the error
> message
> is that c is not an lvalue.

An even stronger hint that the compiler doesn't see any variable c!


Bo Persson

0 new messages