Topic: Single Identifier With Different Associations
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 29 Apr 03 06:29:13 GMT Raw View
In message <7f2735a5.0304250907.5e1d794d@posting.google.com>, Allan W
<allan_w@my-dejanews.com> writes
>David and I read the same question, and took it to mean two completely
>different things.
>
>David apparently thought that Omer was asking, can you declare the
>same variable twice in the same scope?"
>
>I thought Omer was asking, "Can two different variables in two different
>scopes have the same name?"
>
>Omer, if you haven't yet seen a good answer to your question, please
>rephrase it. What is it you really want to know?
And I thought he meant can the same name be declared to mean two
different entities in the same scope. My answer would be yes. Examples:
overloaded functions and the coexistence of a variable and a
class/struct declared with the same name in the same scope.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
Author: Daniel Frey <daniel.frey@aixigo.de>
Date: 29 Apr 03 06:29:26 GMT Raw View
Steve Dewhurst wrote:
> omershad@hotmail.com (Omer Rashid) wrote in message news:<934a761a.0304210012.58135e35@posting.google.com>...
>
>>Hello ,
>> I want to ask a question . I am mainly concerned to this
>>question in C++ . the question says .
>> Can there be a single identifier within a single program with
>>different associations ??
A bit unclear, but I think it is meant to limit the occurrence of the
identifier to a single local scope, right?
> There's always the following:
>
> void f() {
> int i = 12;
> goto i;
> i: ;
> }
Maybe this qualifies, too:
void i( int i ) {}
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schlo_-Rahe-Stra_e 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
Author: allan_w@my-dejanews.com (Allan W)
Date: 28 Apr 03 07:08:06 GMT Raw View
omershad@hotmail.com (Omer Rashid) wrote
> Can there be a single identifier within a single program with
> different associations ??
david.boyle@ed.tadpole.com (Dave Boyle) answered
> To my knowledge there is only one case where more than one
> *declaration* of a local is permitted:
>
> f() { extern int x[]; extern int x[10];...}
Wow.
David and I read the same question, and took it to mean two completely
different things.
David apparently thought that Omer was asking, can you declare the
same variable twice in the same scope?"
I thought Omer was asking, "Can two different variables in two different
scopes have the same name?"
Omer, if you haven't yet seen a good answer to your question, please
rephrase it. What is it you really want to know?
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
Author: scd@semantics.org (Steve Dewhurst)
Date: 28 Apr 2003 09:12:42 -0400 Raw View
omershad@hotmail.com (Omer Rashid) wrote in message news:<934a761a.0304210012.58135e35@posting.google.com>...
> Hello ,
> I want to ask a question . I am mainly concerned to this
> question in C++ . the question says .
> Can there be a single identifier within a single program with
> different associations ??
There's always the following:
void f() {
int i = 12;
goto i;
i: ;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: david.boyle@ed.tadpole.com (Dave Boyle)
Date: 24 Apr 2003 19:23:35 -0400 Raw View
> void abc()
> {
> and in it we can declare
> int x;
> and then again
> int x;
> }
To my knowledge there is only one case where more than one
*declaration* of a local is permitted:
f() { extern int x[]; extern int x[10];...}
This is legal C and I'm guessing that it's more likely than not legal
C++ too (someone who knows the C++ standard better than I could
probably verify/refute this).
Without the extern storage class (as in your pseudo code), these two
declarations would become definitions and that would be illegal.
Furthermore, the two declarations in f() in no way constitute a
"single identifier with different associations".
HTH,
Dave
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Tommy" <ltk_RE_MO_VE_@libero.it>
Date: 24 Apr 2003 19:24:10 -0400 Raw View
"Omer Rashid" <omershad@hotmail.com> wrote
> void abc()
> {
> and in it we can declare
> int x;
> and then again
> int x;
> }
> Can this happens in any case in C++
> Our teacher says yes but he has not told us the answer . Can any body
> help me in this case . There should be no subprogram call or any other
> scope like this {} in which the variable of the same name exist .
Most obviuos solution.
void abc()
{
int x;
{
int x;
}
}
If it cannot be between { and }, you could use a for:
void abc()
{
for(int x=0; x<5 ;++x);
for(int x=33;x<40;++x);
}
I cannot think of anyting else....
Tommy
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: 24 Apr 2003 19:39:22 -0400 Raw View
Omer Rashid wrote:
>
> void abc()
> {
> // and in it we can declare
> int x;
> // and then again
> int x;
> }
> Can this happens in any case in C++
No. You need a new scope, but...
> ...There should be no subprogram call or any other
> scope like this {} in which the variable of the same name exist.
...you can introduce a new scope with "for" and "if" statements
(and I seem to recall someone saying you can do it with "while"
statements as well) so the following is valid.
void abc()
{
if (int x = ...)
Foo(x);
// and then again
if (int x = ...)
Bar(x);
}
It doesn't use braces, {}, so it might be what your teacher is
thinking of, but it does introduce two new scopes, so I think
your teacher is misleading you.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: allan_w@my-dejanews.com (Allan W)
Date: 24 Apr 2003 19:39:40 -0400 Raw View
omershad@hotmail.com (Omer Rashid) wrote
> Can there be a single identifier within a single program with
> different associations ??
> means that in C++
> this is a block of subprogram
> void abc()
> {
> and in it we can declare
> int x;
> and then again
> int x;
> }
> Can this happens in any case in C++
> Our teacher says yes but he has not told us the answer .
Your teacher is right.
The obvious case is seperate subprograms:
void foo() {
int x;
for (x=0; x<10; ++x) {
/* ... */
}
}
float bar() {
float x = 0.0;
for (int i=1; i<=10; ++i)
x += sqrt(i);
return x;
}
Obviously, the two variables "x" have nothing to do with each other.
What your teacher was probably getting at is something called "local
scope." Look in your textbook (or any good book about C++) for "scope"
and read all about it.
Something to think about: is the following code legal? If so, what
does it write? If not, why not?
int z=10;
void baz() {
float z=20.0;
for (int i=0; i<10; ++i) {
std::string z("Hello");
// ...
}
std::cout << z << std::endl;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: omershad@hotmail.com (Omer Rashid)
Date: 23 Apr 03 09:36:30 GMT Raw View
Hello ,
I want to ask a question . I am mainly concerned to this
question in C++ . the question says .
Can there be a single identifier within a single program with
different associations ??
means that in C++
this is a block of subprogram
void abc()
{
and in it we can declare
int x;
and then again
int x;
}
Can this happens in any case in C++
Our teacher says yes but he has not told us the answer . Can any body
help me in this case . There should be no subprogram call or any other
scope like this {} in which the variable of the same name exist .
Omer Rashid
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]