Connecting Tech Pros Worldwide Help | Site Map

surprised that this is valid...

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 18th, 2008, 05:26 PM
werasm
Guest
 
Posts: n/a
Default surprised that this is valid...

Hi all,

I've per chance noticed that this code compiles without warnings:

struct x
{
x(): y_( y_ ){}

int y_;
};

I've used gcc 4.1.0, comeau online and all compilers available at
dinkumware online.

Are there cases where similar code (a member initialising itself with
itself) would make sense? I was surprised when I discovered this.

Regards,

Werner

  #2  
Old March 18th, 2008, 10:35 PM
Greg Herlihy
Guest
 
Posts: n/a
Default Re: surprised that this is valid...

On Mar 18, 11:14*am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
Quote:
Note that if there is an x or a y (or in your case a y_) in a larger
scope, then it will use the larger scoped one instead:
>
int x = 0, y = 0;
>
void function () {
* int x(x); // now it uses x from above
* int y = y; // same here
>
}
No, both x and y are still being initialized with their own
indeterminate values - the x and y declarations in the outer scope
make no difference here. In short, the inner x and y declarations hide
the outer ones.

There is one notable exception, however:

const int i = 2;
{
int i[i];
}

In this case, the inner "i" declares an array of two ints.

Greg

  #3  
Old March 18th, 2008, 11:26 PM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: surprised that this is valid...

On Mar 18, 6:26 pm, Greg Herlihy <gre...@mac.comwrote:
Quote:
On Mar 18, 11:14 am, "jason.cipri...@gmail.com"
>
<jason.cipri...@gmail.comwrote:
Quote:
Note that if there is an x or a y (or in your case a y_) in a larger
scope, then it will use the larger scoped one instead:
>
Quote:
int x = 0, y = 0;
>
Quote:
void function () {
int x(x); // now it uses x from above
int y = y; // same here
>
Quote:
}
>
No, both x and y are still being initialized with their own
indeterminate values - the x and y declarations in the outer scope
make no difference here. In short, the inner x and y declarations hide
the outer ones.
Oops, thanks for catching that, Greg.
Quote:
There is one notable exception, however:
>
const int i = 2;
{
int i[i];
}
>
In this case, the inner "i" declares an array of two ints.
>
Greg
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.